Common C++ Compiler and Linker Errors

The list below shows some of the common C++ compiler and linker errors that you are likely to see when working on the projects for this course. This page is a continual work in progress.

If you have suggestions for errors that should be included in this document or have questions or suggestions for improving the document please email Mr. Frey

Definitions

Commonly used words and phrases found in the compiler and linker error messages.
  1. identifier -- the name of a class, struct, function or variable
  2. collect2: ld returned 1 exit status -- usually found as the last line of the error. This phrase indicates that you have a linker (ld) error, not a compiler error. Linker errors occur when g++ tries to combine all of your .o files into an executable file. Linker errors CANNOT be fixed by guarding header files or by changing which header files are included in your .cpp file.
  3. non-aggregate type -- classes and structs are generically called "aggregate" types. If you get an error indicating that your class is a "non-aggregate type", then the compiler has not seen your class definition and doesn't recognize your class as such.
  4. read-only structrure -- refers to a const object. This phrase is generally found in a compiler error when you are trying to change a data member in a const object.

Compiler Errors

undeclared identifier

  1. Example doy.cpp: In function `int main()': doy.cpp:25: `DayOfYear' undeclared (first use this function) doy.cpp:25: (Each undeclared identifier is reported only once for each function it appears in.) doy.cpp:25: parse error before `;' token
  2. Meaning
    You used "DayOfYear" in your code, but the compiler has not seen a definition for "DayOfYear". It doesn't know what "DayOfYear" is.
  3. Usual Causes
    1. You forgot to include the header file that defines the class/struct/function/etc
    2. You misspelled the name of the identifier

cout undeclared

  1. Example xyz.cpp: In function `int main()': xyz.cpp:6: `cout' undeclared (first use this function) xyz.cpp:6: (Each undeclared identifier is reported only once for each function it appears in.)
  2. Meaning
    This is really a special case of "undeclared identifier".
  3. Usual causes
    1. You forgot to include <ostream>
    2. You forgot "using namespace std;"

jump to case label

  1. Example switch.cpp: In function `int main()': switch.cpp:14: jump to case label switch.cpp:11: crosses initialization of `int y'
  2. Meaning
    Your code tried to jump to a case label
  3. Usual Causes
    1. You declared a variable within a "case" inside a switch. This error is fixed by enclosing your code for the case inside of braces.

discards qualifier

  1. Example myfile.cpp: In function `int main()': myfile.cpp:20: passing `const DayOfYear' as `this' argument of `void DayOfYear::Set(int, int)' discards qualifiers
  2. Meaning
    You have an inconsistency with the use of "const"
  3. Usual Causes
    1. A non-const member function is being invoked for a const object
    2. A const object is being passed as a non-const parameter
    3. A const member function calls a non-const member function
    4. See the CMSC 202 lecture notes on the use of const

multi-line string / untermainated string

  1. Example This short program #include <iostream> #include <string> using namespace std; int main( ) { cout << "Bob is my buddy; cout << " and so is Mary" << endl; } causes these compiler errors string.cpp:7:12: warning: multi-line string literals are deprecated string.cpp: In function `int main()': string.cpp:7: `so' undeclared (first use this function) string.cpp:7: (Each undeclared identifier is reported only once for each function it appears in.) string.cpp:7: parse error before `Mary' string.cpp:8:28: warning: multi-line string literals are deprecated string.cpp:8:28: missing terminating " character string.cpp:7:12: possible start of unterminated string literal
  2. Meaning
    The compiler thinks that you are trying to create a multi-line string which is no longer a supported feature of the language
  3. Usual Causes
    1. You're missing a quote mark at the end of one of your strings

member initializers will be reordered

  1. Example mil.cpp: In constructor `Fraction::Fraction(int, int)': mil.cpp:14: warning: member initializers for `int Fraction::m_denominator' mil.cpp:14: warning: and `int Fraction::m_numerator' mil.cpp:19: warning: will be re-ordered to match declaration order
  2. Meaning
    The order in which data members were initialized in the constructor's member initialization list does not match the order in which the data members were defined in your class. Note that this warning does not appear unless you use the -ansi -Wall switches as you are required to do.
  3. Usual Causes
    1. The cause is self-explanitory. Reorder the data members in either the class definition or the member initialization list.

ostream copy constructor is private

  1. Example /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/bits/ios_base.h: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)': /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/bits/ios_base.h:424: `std::ios_base::ios_base(const std::ios_base&)' is private mil.cpp:28: within this context mil.cpp: In function `int main()': mil.cpp:28: initializing argument 1 of `void Fraction::Print(std::basic_ostream<char, std::char_traits<char> >) const'
  2. Meaning
    You aren't allowed to make a copy of an ostream
  3. Usual Causes
    1. You passed an ostream to a function by value. ostreams must be passed by reference.

comparison between signed and unsigned integer expressions

  1. Example xyz.cpp: In function `int main()': zyz.cpp:54: warning: comparison between signed and unsigned integer expressions
  2. Meaning
    This is a compiler warning that you are comparing ( ==, <, > etc) an integer expression (may be poitive, negative or zero) and an unsigned integer expression (may be positive or zero, but not negative).
  3. Usual Causes
    1. In our projects, this warning usually arises in a for-loop which is looping through all elements of a vector. For example, assuming "grades" is a vector of some kind, the warning is generated by this code for (int i = 0; i < grades.size( ); i++ { // body of the for-loop here } because vector's size( ) function returns an unsigned int. To fix this problem simply define 'i' as an unsigned int too, as in
      for( unsigned int i; i < grades.size( ); i++)

suggest parentheses around assignment used as truth value

  1. Example xyz.cpp: In function `int main()': xyz.cpp:54: warning: suggest parentheses around assignment used as truth value
  2. Meaning
    This is a suggestion from the compiler that you add parenthesis around an assignment statement that used as a condition in an if/while/for, etc. This is usually NOT what you meant to do.
  3. Usual Causes
    1. This warning is usually caused by using "=" instead of "==" in an if-statement as in
      if ( length = maxLength ) when what you meant was
      if ( length == maxLength)

Linker Errors

undefined reference

  1. Example /tmp/cc2Q0kRa.o: In function `main': /tmp/cc2Q0kRa.o(.text+0x18): undefined reference to `Print(int)' collect2: ld returned 1 exit status
  2. Meaning
    Your code called the function Print, but the linker could not find the code for it in any .o file
  3. Usual Causes
    1. You forgot to link the .o file that contains the function
    2. You misspelled the name of the function
    3. You spelled the name of the function correctly, but the parameter list is different in someway

undefined reference to 'main'

  1. Example /usr/lib/crt1.o: In function `_start': /usr/lib/crt1.o(.text+0x18): undefined reference to `main' collect2: ld returned 1 exit status
  2. Meaning
    This is a special case of "undefined reference"
  3. Usual Causes
    1. You compiled a .cpp that does not contain main( ), but forgot to use the -c switch. When compiling .cpp files that do not contain main( ), use the command line g++ -ansi -Wall -c myfile.cpp

Dennis Frey
Last modified: Mon Nov 15 10:05:34 EST 2004