back to the homepage

g++ compiler error messages for idiots

i am not the world's best programmer, and don't have the world's greatest memory. i do, however, have a natural talent for creating bugs and error messages which is unsurpassed (at least in this lab). these are here as they are errors i have fallen into before and will probably fall into again. they might be useful for other people. i have looked on the web for beginner's explainations of g++ compiler messages, and the best i could find was this:; if you can't find what you are looking for here you might want to try their list.

Error messageExplanation
error: expected `;' before ':' token You've finished a line with a : instead of a ;
error: 'cerr' was not declared in this scope You've either forgotten to #include <iostream>, or you've forgotten the line using namespace std, or something like that.
cannot resolve overloaded function `return_id' based on conversion to type `int'

or something like that. you look in the code, return_id returns the id, which is an integer. so it's not being converted. and it's not overloaded. your code looks something like i=return_id; and you feel confused - surely that is what it should have done? But no! the mistake you have made is to miss out the brackets: what you actually wanted to say is i=return_id();

no match for `std::basic_ostream<char, std::char_traits<char> > & << <unknown type>' operator It's forgetting the brackets() again, this time in a cout statement.
request for member `save_map' in `map', which is of non-aggregate type `goal ()()' In this case, i'd got a constructor for the class which was void, but was declaring the class with brackets after the name (so i'd said goal map() where i wanted to say goal map).
undefined reference to 'filename If the error is in a file which ends .o it's a linker problem not a compiler problem: there's something up with the makefile. things i've come across so far are
  • having things link in the wrong order - linking the libraries before my code
  • having two mains which don't match up
undefined reference to `class_name::class_name[in-charge](parameters) or undefined reference to `class_name::function_name[in-charge](parameters) This is a linker problem - the error is with a file called .o - and the cause is similar to the problem outlined above. (Linking the libraries before the code that used the libraries). The order for Makefiles should be:
Create the object files and/or libraries which are required.

  1. Compiler and compiler flags (g++ -ggdb -Wall ... )
  2. Include directries (the -I/dir/dir/dir/ where your .h files are)
  3. Object files (all your .os)
  4. Executable file (the one with main in, ends .cc or .cpp)
  5. Library directories (the -L/dir/dir/dir where your libwhatsit.a files are)
  6. Library names (-lwhatsit)
  7. -o whatever_you_want_the_program_to_be_called
In function `_start': : undefined reference to `main' collect2: ld returned 1 exit status make: *** [test_data] Error 1 This one had me thinking WTF? Undefined reference to "main"? I've got a main right there damn it!
But of course I didn't. Not really. I'd forgotten to include the file with the main in it on the objects line in my Makefile so it wasn't being turned into a .o file and wasn't being linked properly.
error: variable 'std::ofstream outfile' has initializer but incomplete type This message means you've forgotten to include fstream.
error: 'int classtype::operator==(const classtype&, const classtype&)' must take exactly one argument This is because you're trying to overload an operator which takes more than one parameter (e.g., ==, +=, +, -, *, / < ... something like that) as a member function like this:
int classtype::operator==(const classtype &c1, const classtype &c2) {
return (c1.member==c2.member);}
By defining the operator as a member function it expects to work on the variable this (so for example int classtype::operator==(const classtype &c1) { return (this->member==c1.member);}.
The answer is to a) either redefine the operator outside of the class (as a global function) or to re-write it using this. There is a table on the format these operator overloading things take as both member functions and global functions on about 2/3 of the way down this page.
driver.o: In function `main': ./driver.cpp:63: undefined reference to `function_name(param_class*, int*, blah*, double)' collect2: ld returned 1 exit status make: *** [driver] Error 1 This error can come up when you have extra functions not in classes (bad form, eh?) in the same file as your main. The signs are that the problem isn't with the compiler, but with the linker. The function prototype at the top of your file is fine, but the function definition lower down has the wrong name and/or arguments.