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.
- identifier -- the name of a class, struct, function or variable
- 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.
- 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.
- read-only structure -- 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
- 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
- Meaning
You used "DayOfYear" in your code, but the compiler has not seen a
definition for "DayOfYear". It doesn't know what "DayOfYear" is.
- Usual Causes
- You forgot to include the header file that
defines the class/struct/function/etc
- You misspelled the name of the identifier
cout undeclared
- 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.)
- Meaning
This is really a special case of "undeclared identifier".
- Usual causes
- You forgot to include <ostream>
- You forgot "using namespace std;"
jump to case label
- Example
switch.cpp: In function `int main()':
switch.cpp:14: jump to case label
switch.cpp:11: crosses initialization of `int y'
- Meaning
Your code tried to jump to a case label
- Usual Causes
- 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
- Example
myfile.cpp: In function `int main()':
myfile.cpp:20: passing `const DayOfYear' as `this' argument of `void DayOfYear::Set(int, int)' discards qualifiers
- Meaning
You have an inconsistency with the use of "const"
- Usual Causes
- A non-const member function is being invoked for a const object
- A const object is being passed as a non-const parameter
- A const member function calls a non-const member function
- See the CMSC 202 lecture notes on the use of
const
multi-line string / unterminated string
- Example
This short program
#include
#include
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
- Meaning
The compiler thinks that you are trying to create a multi-line
string which is no longer a supported feature of the language
- Usual Causes
- You're missing a quote mark at the end of one of your strings
member initializers will be reordered
- 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
- 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.
- Usual Causes
- The cause is self-explanatory. Reorder the data members
in either the class definition or the member initialization list.
ostream copy constructor is private
- 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
>::basic_ios(const std::basic_ios >&)':
/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 >) const'
- Meaning
You aren't allowed to make a copy of an ostream
- Usual Causes
- You passed an ostream to a function by value. ostreams must be passed by reference.
comparison between signed and unsigned integer expressions
- Example
xyz.cpp: In function `int main()':
zyz.cpp:54: warning: comparison between signed and unsigned integer expressions
- Meaning
This is a compiler warning that you are comparing ( ==, <, > etc) an integer
expression (may be positive, negative or zero) and an unsigned integer expression
(may be positive or zero, but not negative).
- Usual Causes
- 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
- Example
xyz.cpp: In function `int main()':
xyz.cpp:54: warning: suggest parentheses around assignment used as truth value
- 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.
- Usual Causes
- 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
- Example
/tmp/cc2Q0kRa.o: In function `main':
/tmp/cc2Q0kRa.o(.text+0x18): undefined reference to `Print(int)'
collect2: ld returned 1 exit status
- Meaning
Your code called the function Print, but the linker could not
find the code for it in any .o file
- Usual Causes
- You forgot to link the .o file that contains the function
- You misspelled the name of the function
- You spelled the name of the function correctly, but the
parameter list is different in someway
undefined reference to 'main'
- Example
/usr/lib/crt1.o: In function `_start':
/usr/lib/crt1.o(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
- Meaning
This is a special case of "undefined reference"
- Usual Causes
- 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: Tue Dec 28 15:00:02 EST 2004