Common Syntax Errors in C++ (and responses from the g++ compiler)

last revision: 12-SEP-97

Please send suggestions, additions, and corrections to mfms@csc.ncsu.edu.

Some suggestions in tracking down the meaning of error messages from the preprocessor (cpp), compiler (gcc), and loader (ld). For execution-time errors such as Segmentation fault or Bus error, see the information on using the debugger.

The list is in random order at this point, but some structure will be imposed when it gets longer.

Actual Error: You neglect to put ClassName:: in front of the name of a member function when defining it.
g++ response: Undefined function myX, where myX is a (private) data member used in the function.

Actual Error: You provide a constructor that requires arguments for a class, say class MyClass, but no null constructor. In the client code you either declare an array of class objects,

MyClass items[LENGTH];
or an object without initialization,
MyClass item;
In other words, you disable the default null constructor and use a declaration that requires it.
g++ response: No matching function call to ClassName::ClassName(), referring to a line in which an array or an object of class ClassName is declared.

Actual Error: Using == to compare two objects belonging to some user-defined class (assuming the operator == is not overloaded for that class)
g++ response: No matching call for operator ==(ClassName, Classname)

Actual Error: Using = instead of == for equality comparison.
g++ response: warning: suggest parentheses around assignment used as truth value

Actual Error: You leave out the semicolon at the end of a class declaration.
g++ response: Parse error before ..., referring to the next item after the declaration (which is often in another file because the declaration may be at the end of a header, that is, .h file). C++ allows a list of variables before the semicolon after a class declaration, e.g.

class A { int myP; public: A(); print(); } x, y, z;
where x, y, z are now declared as objects of class A. The parse error results because g++ has encountered a keyword, delimiter, or class name instead of the expected identifier. You may get
 return type specification for constructor invalid
if the first thing after the class declaration is the definition of the constructor.

Actual Error: You leave out the () when calling a class member function with no arguments, as in x.foo where x is of class Bar and the declaration void foo(); is part of Bar's definition.
g++ response: Often none whatsoever, but strange behavior can occur at run time because x.foo denotes a pointer to the code for the function foo rather than a call to it.

Actual Error: Putting a & after the return type of a function in the declaration, but forgetting it in the definition or vice-versa (return by reference in one place but not the other).
g++ response:

new declaration ... ambiguates old declaration ...
The ...'s represent the offending function prototypes. The line numbers on which they occur are also given. The same message is used whenever two function prototypes differ only in their return type (C++ regards these as being the same function).

Actual Error: Using the scope resolution operator (::) with the class name when defining a friend function that overloads an operator for a class, e.g.

    friend Foo operator+(const Foo& L, const foo& R);
in the class declaration, and
    Foo Foo::operator+(const Foo& L, const Foo& R);
in the implementation.
g++ response:
    Foo::operator (unary +)(const Foo &, const Foo &) must take either zero
    or one argument
g++ regards Foo::operator+(const Foo &, const Foo &) as distinct from operator+(const Foo &, const Foo &). The former can take at most one argument because the left (or only, in case of unary) operand is the class object itself.

Actual Error: Using both square brackets and * to declare an array of arbitrary length, as in

    int* myArray[];
when all you want is a one-dimensional array.
g++ response: (when you attempt, for example, to pass the array to a function foo(int a[]))
     passing `int **' as argument 1 of `foo(int *)'
To the compiler int * x is equivalent to int x[] and int ** x is equivalent to int * x[]. If you try to do something like myArray[i] + 1, you will get
    invalid operands `int *' and `int' to binary `operator +'
An element of an array declared as int* myArray[] is of type int*.

Actual Error: Using the convention of the Perry/Levin text, you declare a private data member Node* link_; and a public access function Node* link() const;. In one of your references to the private member, you accidentally leave off the underscore (_) at the end.
g++ response:

     assignment to `Node *' from `Node * (Node::*)() const'
where the statement being referred to was
curr = curr->link;
and curr has been declared Node*. The compiler treats link as a pointer to the function link(). Note: The compiler will not complain about equality/inequality comparisons such as (curr->link != 0) -- it's perfectly ok to compare a function pointer to another or to 0. If this comparison occurs at the top of a loop, your typo will lead to an infinite loop! This is one reason we prefer using myItem as a data member name rather than item_.

Actual Error: Forgetting to protect a header file against duplicate definitions (see page 405 of Perry/Levin).
g++ response:

redefinition of `class DigitList'
or whatever the class being defined was.

Actual Error:Using . instead of -> to access a member from a pointer variable, for example, saying myHead.myLink in a List class member function.
g++ response:

request for member `myLink' in `DigitList::myHead', which is of non-aggregate
 type `Node *'
An aggregate type is a struct or class, that is, anything that could have more than one data member. A pointer does not qualify (it only occupies one machine word regardless of what it points to) nor does an integer (you'd get a similar message if you did something like int x = 0; x.mySign = '+';).

Actual Error:Using -> instead of . to access a member a class object (not a pointer), for example, saying right->myHead in a List class member function that has a parameter const List& right.
g++ response:

base operand of `->' is not a pointer

Actual Error: Compiling one .o file of a program with multiple translation units on a different machine architecture than that of the current machine).
g++ (loader) response:

Bad magic number
and the .o file is specified.

Actual Error:Leaving out the const at the end of a function header in the implementation (when there is one in the header). For example, the Token class has a member function declared as

   unsigned
   value() const;
in Token.h and defined as
   unsigned
   Token::value()
   { ...
   }
in Token.cpp
g++ response:
prototype for `unsigned int Token::value()' does not match any in class `Token'
candidate is: unsigned int Token::value() const

Actual Error:Leaving out the const at the end of a function header in the class declaration (when there is one in the implementation). For example, the Token class has a member function declared as

   unsigned
   value();
in Token.h and defined as
   unsigned
   Token::value() const
   { ...
   }
in Token.cpp
g++ response:
prototype for `unsigned int Token::value() const' does not match any in class `Token'
candidate is: unsigned int Token::value()

Actual Error:
g++ response:


Matthias Stallmann ( mfms@csc.ncsu.edu)
Last modified: Tue Mar 16 17:58:23 1999