tags:

views:

341

answers:

6

This very simple code gives me tons of errors:

#include <iostream>
#include <string>

int main() {
    std::string test = " ";
    std::cout << test;
}

I tried to compile it on linux by typing gcc -o simpletest simpletest.cpp on the console. I can't see why it isn't working. What is happening?

A: 

You declared your main() as returning an int yet you have no return statement. Add return 0; and see if that helps. If that doesn't solve your problem, try editing your post to include some representative lines from those errors your getting and maybe we can help you better.

rmeador
the problem was that i was using the wrong compiler... silly mealthough i can see that the missing return statement, even if it didn't throw any warnings or errors, must be bad practice. Thanks
David McDavidson
actually, c++ has a special concession that allows main to not have a return statement even with it declared returning an int. in which case it will implicitly return the proper success value, usually 0.
Evan Teran
@Evan: interesting... I used to declare my main() to be void so I don't have to return something, when I used compilers that supported it. G++ doesn't like that though, so I have to make it be int and return 0.
rmeador
It was always wrong to declare main() to be void, unless you intentionally rely an a particular implementation's extensions.
Windows programmer
+8  A: 

Try using 'g++' instead of 'gcc'.

grieve
And add return 0; as rmeador pointed out.
grieve
that was it. such a silly mistake! thanks
David McDavidson
actually, c++ has a special concession that allows main to not have a return statement even with it declared returning an int. in which case it will implicitly return the proper success value, usually 0.
Evan Teran
@Evan: I didn't realize that. I'll have to test out my various C++ compilers and see which ones actually implement that feature.
grieve
A function declared to return a value (say, int) but that does not actually have a return statement will compile, *should* cause a warning but not an error, and will return garbage values when run. I believe the "special concession" is to have main() return a sensible 0 instead of garbage.
Max Lybbert
+5  A: 

Try:

g++ -o simpletest simpletest.cpp
Robert Gamble
that's right, it worked, thanks!
David McDavidson
+5  A: 

Try with g++ -o simpletest simpletest.cpp. gcc is the C compiler, while g++ is the C++ compiler which also links in the required C++ libraries.

Additionally, you will have to add a return 0; at the end of your main() function.

MP24
actually, c++ has a special concession that allows main to not have a return statement even with it declared returning an int. in which case it will implicitly return the proper success value, usually 0.
Evan Teran
gcc is actually the GNU compiler collection; you can still compile C++ code with gcc, but you must explicitly link in the C++ standard library with the -lstdc++ option.
Adam Rosenfield
C99 also has the same awful concession to abysmal coding practices (that is, you can omit a return from the end of main() and it is equivalent to a return(0); at the end of main()).
Jonathan Leffler
+2  A: 

if your compiler is picky you may want to add that all important return 0; at the end there

RAGNO
Not necessary in standard C++ -- nor in C99. That said, I'd add the explicit return; the implicit return is pretty nasty.
Jonathan Leffler
+7  A: 

To add to what others have said: g++ is the GNU C++ compiler. gcc is the GNU compiler collection (not the GNU C compiler, as many people assume). gcc serves as a frontend for g++ when compiling C++ sources. gcc can compile C, C++, Objective-C, Fortran, Ada, assembly, and others.

The reason why it fails trying to compile with gcc is that you need to link in the C++ standard library. By default, g++ does this, but gcc does not. To link in the C++ standard library using gcc, use the following:

gcc -o simpletest simpletest.cpp -lstdc++
Adam Rosenfield
That solves link errors but the original poster complained about compile errors ;
Windows programmer
The original poster didn't specify whether he's getting compiler or linker errors, but if you compile that code yourself using gcc you'll get linker errors.
Adam Rosenfield