User Tools

Site Tools


start

Common G++ Errors

Summary

This is a guide to help you CS35ers make sense of g++ and its often cryptic error messages.
If you discover an error not listed here feel free to edit this wiki,
or send your error along with source code and an explanation to me - grawson1@swarthmore.edu

Weird Errors

If you are getting strange compiler errors when your syntax looks fine, it might be a good idea to check that your Makefile is up to date
and that you are including the proper .h files. Sometimes a missing } or ; or ) will yield some scary errors as well.
Lastly, save all files that you might have changed before compiling.

Bad Code Sample

What's wrong with the following code? (Hint: there are two errors)

#include<iostream>
using namespace std;
 
int main(){
 
  int foo;
 
  for(int i = 0, i<100, i++) {
    foo++;
    cout << foo << endl;
  }
  return 0;
}

When compiled this yields:

junk.cpp:9: error: expected initializer before '<' token
junk.cpp:13: error: expected primary-expression before 'return'
junk.cpp:13: error: expected `;' before 'return'
junk.cpp:13: error: expected primary-expression before 'return'
junk.cpp:13: error: expected `)' before 'return'

First, the parameters of the for loop need to be separated by semicolons, not commas.

for(int i = 0; i<100; i++) {
    foo++;
    cout << foo << endl;
  }

Now look at this sample output of the corrected code:

-1208637327
-1208637326
-1208637325
-1208637324
-1208637323
-1208637322

Why the weird values? Because we never initialized foo before incrementing it.

 int foo = 0;

‘cout’ was not declared in this scope

Two things to check here:

(1) Did you add

 #include<iostream> 

to your list of headers? (2) Did you add

 using namespace std; 

after your #includes?

‘printf’ was not declared in this scope

Add

 #include<cstdio> 

to your list of headers. Note if you are coming from C programming, adding #include<cstdio> is preferred in C++ over #include <stdio.h>.

Cannot Pass Objects of non-POD Type

junk.cpp:8: warning: cannot pass objects of non-POD type 'struct std::string' through '…'; call will abort at runtime
junk.cpp:8: warning: format '%s' expects type 'char*', but argument 2 has type 'int'

What this usually means is that you forgot to append .c_str() to the name of your string variable when using printf.
This error occurred when trying to compile the following code:

int main(){
 
  string foo = "dog";
  printf("This animal is a %s.\n",foo);
  return 0;
}

Simply appending to .c_str() to “foo” will fix this:

printf("This animal is a %s.\n",foo.c_str());

The reason you got this error is because printf is a C function and C handles strings differently than C++

Invalid Use of Member

junk.cpp:8: error: invalid use of member (did you forget the '&' ?)

What this usually means is that you forget to add () to the end of a function call.
Ironically, every time I see this it is never because I forgot the '&'. This error occurred when trying to compile the following code:

int main(){
 
  string foo = "dog";
  printf("This animal is a %s.\n",foo.c_str);
 
  return 0;
}

Simply adding the open and close parentheses () will take care of this for you:

printf("This animal is a %s.\n",foo.c_str());

Request for Member 'Foo' in 'Bar', which is of non-class type 'X'

trycredit.cpp:86: error: request for member 'print' in 'card', which is of non-class type 'CreditCard*'

What this usually means is that you are using a '.' between the class pointer and the function you are trying to call. Here is an example from the CreditCard lab:

void useCard(CreditCard *card, int method) {
  //Used to access the methods of the class
 
  if (method==1) {
    card.print();
  }

Since card is a CreditCard* we need → rather than . Fixed:

  if (method==1) {
    card->print();
  }

Undefined Reference to V Table

This error usually means you need to add a destructor to your myClass.cpp/myClass.inl code. If you don't want to implement a real destructor at this point, you can write something like this:

myClass::~myClass(){}

So long as the destructor exists, you should now be able to compile fine. Of course, implement a real destructor at a later point.

start.txt · Last modified: 2011/01/25 05:01 (external edit)