Common Compiler Errors

Use your browser's 'Find...' menu option to search through this page for a `unique' word in your error message.

g++ can give some compiler/linker errors that don't seem to make a whole lot of sense. What follows are some common errors that it gives and what will cause them.. For most errors, g++ will start the error message by telling you what function it has discovered the error in. Generally this will be something like:

cppfile.cpp: In function `int main()':
This is NOT the actual error message. It is just telling you which file and function it found the error in.

Disclaimer: The code given below isn't necessarily the best way to code. It is only given to point out errors.

The list (numbers to the right of the code are line numbers. They aren't actually in the code):

  1. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:7: no match for `_IO_istream_withassign & << float &'     
    
    Pertinent Code:
       cin << fValue;                                      7
    
    Cause:
    The insertion operator has been used instead of the extraction operator. Note: the 'float &' might be something else in your error.

    Solution:
    Replace the << with >>.

  2. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:6: no match for `_IO_ostream_withassign & >> char[7]'    
    
    Pertinent Code:
       cout >> "Hello!" >> endl;                            6
    
    Cause:
    The extraction operator has been used instead of the insertion operator. Note: the 'char[7]' might be something else in your error.

    Solution:
    Replace the >> with <<.

  3. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:7: parse error before `>'  
    
    Pertinent Code:
       cout << "Hello!" << endl                                6
       cin >> fValue;                                          7
    
    Cause:
    There is a missing semicolon. This is g++'s "catch-all" error message. If it can't figure out what is wrong, it will give this message. It is telling you that it has found some error before the >. Generally this means that you are missing a semicolon on the line of code above that.

    You might also get a parse error before an equals sign which is caused by the same problem. In fact, you can get parse errors before just about anything, and they often mean that the line of code above that is missing a semicolon.

    Solution:
    Put in the semicolon.

  4. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:5: warning: unused variable `float fValue'          
    
    Pertinent Code:
    int main(void)                                               3
    {                                                            4
       float fValue;                                             5
       cout << "Hello!" << endl;                                 6
                  
       return 0;
    }
    
    Cause:
    You aren't using the fValue variable anywhere inside your code. g++ is warning you that this is a problem. Warnings don't actually keep your code from compiling. It will still compile; however, warnings usually indicate a problem somewhere so you need to get rid of them.

    Solution:
    If you aren't actually needing that variable, don't declare it. Otherwise, make sure that you don't have a typo anywhere in your code that is causing this error to show up. For instance, if you have used 'fvalue' somewhere in your code you will probably get two errors. One telling you that you are using 'fvalue' undeclared, and a warning telling you that you aren't using 'fValue'.

  5. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:7: `fvalue' undeclared (first use this function)
    cppfile.cpp:7: (Each undeclared identifier is reported only once
    cppfile.cpp:7: for each function it appears in.)
    cppfile.cpp:5: warning: unused variable `float fValue'   
    
    Pertinent Code:
    int main(void)                                                  3
    {                                                               4
       float fValue;                                                5
       cout << "Hello!" << endl;                                    6
       cin >> fvalue;                                               7
    
    Cause:
    All of the error lines that list line 7 as the problem line are one error. That isn't three different errors on line 7. It is telling you that you are trying to use the variable fvalue and you have not declared it yet. In addition, there is a warning that fValue is not being used.

    Solution:
    Make sure that the variable name given in the declaration exactly matches its use in your C++ code.

  6. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:7: warning: converting `ostream & (*)(ostream &)' to 
                   `ios & (*)(ios &)' is a contravariance violation      
    
    Pertinent Code:
       cin >> fValue >> endl;                                  7
    
    Cause:
    endl is only defined for use with cout.

    Solution:
    Get rid of the >> endl on the cin line. ONLY use endl with cout.

  7. Error:
    cppfile.cpp:10: undefined or invalid # directive
    cppfile.cpp: In function `int main()':  
    
    Pertinent Code:
    #icnlude <iostream>                                           10
    
    Cause:
    Note that the error is listed before the 'In function `int main()'. This means that the error occured before the beginning of the main function. In this case, include was misspelled.

    Solution:
    Spell include properly.

  8. Error:
    cppfile.cpp:3: person.h: No such file or directory 
    
    Pertinent Code:
    #include "person.h"                                              3
    
    Cause:
    A file with the name person.h does not exist in the current directory. This could be because the filename isn't spelled correctly. Make sure that you have the capitalization the same. It is common to capitalize the first letter of header files that contain structures/classes. Also, the file might exist, but be in a different directory. It needs to be in the same directory.

    Solution:
    Make sure that there is a file in the same directory as the source file that has the name that you have inside the quotes on the include line.

  9. Error:
    cppfile.cpp:5: semicolon missing after declaration of `Person'
    cppfile.cpp:6: extraneous `int' ignored
    cppfile.cpp:6: semicolon missing after declaration of `struct Person'    
    
    Pertinent Code:
    In cppfile.cpp:
    
    #include "Person.h"                                              3
                                                                     4
    int main(void)                                                   5
    {                                                                6
    
    
    In Person.h:
    
    struct Person
    {
       string strFirstName;
       string strLastName;
    } 
    
    
    Cause:
    All structures/classes need to have a semicolon after their definition (this is IN the header file). As soon as the compiler see the 'int' in 'int main(void)' it knows that whatever came before it (the structure definition in the header file) should have had a semicolon at the end.

    Solution:
    Put in the semicolon (in the header file, after the closing curly brace of the struct/class).

  10. Error:
    In file included from cppfile.cpp:3:
    Person.h:8: parse error before `:'
    cppfile.cpp:8: syntax error before `<'
    cppfile.cpp:9: syntax error before `>'    
    
    Pertinent Code:
    In cppfile.cpp:
    
    #include "Person.h"                                              3
                                                                     4
    int main(void)                                                   5
    {                                                                6
       float fValue;                                                 7
       cout << "Hello!" << endl;                                     8
       cin >> fValue;                                                9
    
    In Person.h:
    
    struct Person
    {
       string strFirstName;                                          6
       string strLastName;                                           7
    }:                                                               8
    
    Cause:
    The colon at the end of Person. The other errors are 'cascading errors' that show up because of the first error.

    Solution:
    Change the colon to a semicolon.

  11. Error:
    In file included from cppfile.cpp:3:
    Person.h:12: unbalanced `#endif'            
    
    Pertinent Code:
    #infdef PERSON_H                                                 1
    #define PERSON_H                                                 2
                                                                     3
    #include <string>                                                4
                                                                     5
    struct Person                                                    6
    {                                                                7
       string strFirstName;                                          8
       string strLastName;                                           9
    };                                                              10
                                                                    11
    #endif                                                          12
    
    Cause:
    On line 1, #ifndef is misspelled. So, the end of the if can't find a beginning that it matches up with.

    Solution:
    Spell ifndef correctly.

  12. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:7: `Person' undeclared (first use this function)
    cppfile.cpp:7: (Each undeclared identifier is reported only once
    cppfile.cpp:7: for each function it appears in.)
    cppfile.cpp:7: parse error before `;'                                    
    
    Pertinent Code:
    In cppfile.cpp:
    
    #include "Person.h"                                             3
                                                                    4
    int main(void)                                                  5
    {                                                               6
       Person persBoss;                                             7
    
    In Person.h:
    
    #ifdef PERSON_H                                                 1
    #define PERSON_H                                                2
    
    Cause:
    ifndef is not spelled correctly. ifdef is the exact opposite of ifndef. ifdef will cause the contents of the header file to be ignored; hence, the Person structure will never be defined.

    Solution:
    Spell ifndef correctly.

  13. Error:
    cppfile.cpp:9: unterminated string or character constant
    cppfile.cpp:6: possible real start of unterminated constant    
    
    Pertinent Code:
       float fValue;                                          5
       cout << "Hello! << endl;                               6
       cin >> fvalue;                                         7
                                                              8
       cout << "Goodbye!" << endl;                            9
    
    Cause:
    You are missing a closing quote. g++ figured this out on line 9. However, it thinks that the problem started on line 6. It generally does a pretty good job. In this case, the closing quote after "Hello" is missing.

    Solution:
    Remember to always end literal strings with the closing quote.

  14. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:9: parse error before `<'   
    
    Pertinent Code:
       cin >> fValue;                                    7
                                                         8
       cout << "fValue is" fValue << endl;               9
    
    Cause:
    In this case, the problem isn't a missing semicolon on the previous line. This time, it is telling you that there is a parse error before the <.. but this time it is actually talking about the second <. An insertion operator has been left out between the literal string and the variable.

    Solution:
    Remember to seperate each "item" that you are printing with the insertion operator.

  15. Error:
    cppfile.cpp:2: conio.h: No such file or directory
    
    Pertinent Code:
    #include <conio.h>                                2
    
    Cause:
    The file conio.h does not exist. Obviously, the error above could be replaced with whatever that you have inside the <> on the include line. That filename simply doesn't exist.

    Solution:
    If it is a header file that you have written yourself, make sure that there is a file in the directory with that exact name (remember, we are case-sensitive). If it is a file like conio.h then you should be aware that Unix doesn't have such a header file.

  16. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:8: no match for `_IO_istream_withassign & >> Person &'
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:223: candidates are: i
    stream::operator >>(streambuf *)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:222:                 i
    stream::operator >>(istream & (*)(istream &))
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:221:                 i
    stream::operator >>(ios & (*)(ios &))
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:220:                 i
    stream::operator >>(long double &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:219:                 i
    stream::operator >>(double &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:218:                 i
    stream::operator >>(float &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:216:                 i
    stream::operator >>(bool &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:214:                 i
    stream::operator >>(short unsigned int &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:213:                 i
    stream::operator >>(long unsigned int &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:212:                 i
    stream::operator >>(unsigned int &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:211:                 i
    stream::operator >>(short int &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:209:                 i
    stream::operator >>(long long unsigned int &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:208:                 i
    stream::operator >>(long long int &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:206:                 i
    stream::operator >>(long int &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:205:                 i
    stream::operator >>(int &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:204:                 i
    stream::operator >>(signed char &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:203:                 i
    stream::operator >>(unsigned char &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:202:                 i
    stream::operator >>(char &)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:201:                 i
    stream::operator >>(signed char *)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:200:                 i
    stream::operator >>(unsigned char *)
    /afs/umr.edu/software/egcs/solaris/include/g++/iostream:199:                 i
    stream::operator >>(char *)        
    
    Pertinent Code:
    #include <iostream>
    #include "Person.h"
    
    int main(void)
    {
       Person p;
       cin >> p;                                           8
    
    Cause:
    The cin object has been used incorrectly. When you use cin to read in a value it actually has to figure out what type of variable it is reading into (whether it be short, long, float, etc.) Other than the base types, cin doesn't know how to read any other types of information. Hence, you can't just tell cin to read in an entire structure or class and expect it to be able to do it. Note, you will get similar error messages if attempt to use cout to print out an entire structure/class.

    Solution:
    You need to tell cin to read in each member of the struct/class individually (unless you overload the extraction operator). [ or, with cout you need to print out each member individually ]

    This can be a tough error to track down. The thing that makes it so difficult is that fact that the important lines have usually scrolled off the screen before you can read them. The very first line of the error message will tell you the exact line that has the problem (line 8 in this case) but you often can't see it on the screen. Here is one way to help find it:

      g++ -Wall -W -s cppfile.cpp -o cppfile     |& more
    
    (that is the vertical bar (or 'pipe' as it is often called) followed by the ampersand, followed by the word 'more'. Note: This works for the csh shell and the tcsh shell. If you don't know what I am talking about, then this probably works for you.)
    This sends the output of g++ (ie., the error messages) to a program ("more") that will only show you one screenful of messages at a time. You can just hit the space bar to page through the error messages, but be sure to note the line number that it has found the problem on before you start paging.

  17. Error:
    In file included from cppfile.cpp:2:
    Person.h:1: unterminated `#if' conditional
    
    Pertinent Code:
    cppfile.cpp:
    
    #include "Person.h"                                  2
    
    Person.h:
    #ifndef PERSON_H                                     1
    #define PERSON_H                                     2
    
    #include <string>
    
    struct Person
    {
       string strFirstName;
       string strLastName;
    };
    
    #end if         
    
    Cause:
    The preprocessor can't find an end to the #if that it has. Hence, it is telling you that it is unterminated.

    Solution:
    Make sure that you have an end for the if. Proper usage is #endif. No spaces. The #endif should be the very last line of text in your header file.

  18. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:7: request for member `m_strFirstName' in `personBoss', which is of
    non-aggregate type `short int'          
    
    Pertinent Code:
    #include <iostream>                                  1
    #include "Person.h"                                    2
                                                           3
    int main(void)                                         4
    {                                                      5
       short personBoss;                                   6
       cout << personBoss.m_strFirstName << endl;          7
       return 0;                                           8
    }       
    
    Cause:
    On this one, the problem isn't on line 8. Line 8 looks great. This message is telling you that are trying to access a member (m_strFirstName in this case) of a variable (personBoss) that is only a short integer. ie., personBoss is a short integer, and short integers don't have "members".

    Solution:
    Look at the variable declaration for personBoss. Make sure that it is of a structure/class type. For Hungarian notation to work you have to make sure that you follow it completely. In this case, personBoss would need to be of type "Person" instead of type "short".

  19. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:14: parse error at end of input        
    
    Pertinent Code:
    int main(void)                                          3
    {                                                       4
       short nCount;                                        5
                                                            6
       for (nCount=0; nCount < 50; nCount++)                7
       {                                                    8
          cout << nCount << endl;                           9
                                                           10
       cout << "Goodbye!" << endl;                         11
       return 0;                                           12
    }                                                      13
    
    Cause:
    There is (most likely) a missing closing curly brace somewhere in your program. This is an interesting error because it is telling you that the error is on a line that doesn't even (necessarily) exist in your program. The thing to remember is that the compiler completely ignores your indentation. So, in the above example it sees the Goodbye message as being inside the for. It sees the return as being inside the for. Then the for ends, and there isn't anything left in the input and it is confused.

    Solution:
    Make sure that you have a matching closing curly brace for every opening brace. If you indent correctly this isn't very hard to do. If you don't indent correctly... you should.

  20. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:12: parse error at end of input                
    
    Pertinent Code:
    #include <iostream>                    1
                                             2
    int main(void)                           3
    {                                        4
       if (1)                                5
       {                                     6
          cout << "Hi << endl;               7
       }                                     8
       cout << Goodbye!" << endl;            9
       return 0;                            10
    }                                       11
    
    Cause:
    Anytime that you get the the parse error, the compiler doesn't think that you have the appropriate closing curly braces to match up with the open curly braces that you have listed. The above example is a tricky one. The problem isn't the existance of the curly braces. They exist, as they should. However, there are TWO places where quotes are missing. At the end of the Hi on line 7, and the beginning of the Goodbye on line 9. So, the computer sees one big cout, where it is going to print Hi << endl; } cout << Goodbye!. Because the closing curly brace is inside the quotes it does not get interpretted by the compiler as being a curly brace.

    Solution:
    I found this one by commenting out the entire if using the C style comments. My code then looked like:

    #include <iostream>
    
    int main(void)
    {
       /*
       if (1)
       {
          cout << "Hi << endl;
       }
       */
       cout << Goodbye!" << endl;
       return 0;
    }        
    
    When I tried to compile this, the compiler gave an error about unterminated string constants which let me track down the bug. If you have an error that you absolutely can't find, try to comment out sections of code and recompile. If you still have the exact same error, you know that it is not inside the section of code that is commented out.

  21. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:8: ANSI C++ forbids comparison between pointer and integer 
    
    Pertinent Code:
       char cChoice;                           5
                                               6
       cin >> cChoice;                         7
       if ("A" == cChoice)                     8
    
    Cause:
    "A" is not a character. It is a string. C++ uses something called a pointer to access character strings. A single character is basically a little integer, and C++ won't let you compare the two.

    Solution:
    If you want to denote a single character, use the single tick marks, as in 'A'.

  22. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:5: warning: implicit declaration of function `int myFunction(...)'
    /var/tmp/cc3ZltCu.o: In function `main':
    /var/tmp/cc3ZltCu.o(.text+0x4): undefined reference to `myFunction'
    collect2: ld returned 1 exit status         
    
    Pertinent Code:
    #include <iostream>                    1
                                             2
    int main(void)                           3
    {                                        4
       myFunction();                         5
       return 0;
    }          
    
    Cause:
    This is a common error. The warning concerning line 5 is the important line. In C/C++ you don't technically have to declare functions (with prototypes). If you don't, C/C++ will implicitly declare the function for you. It will automatically set up a function that returns an int and can take any number of arguments. Since this is virtually NEVER what you want, you should always pay attention to this warning. In short, the error is that C++ can't find your prototype for the function.

    Solution:
    This error can be caused from many different things. In the above example it is quite possible that you have forgotten a #include.
    If you do have the proper #include for the function (make sure that it is the proper header file that has the prototype for the problem function), check to make sure that you have spelled the function name correctly. Remember that C++ is very picky about spelling. If you have the invokation 'myFunction();' and you have the prototype as 'void myfunction();' it won't work.

  23. Error:
    /var/tmp/ccITB4j2.o: In function `main':
    /var/tmp/ccITB4j2.o(.text+0x4): undefined reference to `myFunction(void)'
    collect2: ld returned 1 exit status                           
    
    Pertinent Code:
    #include <iostream>
    
    void myFunction();
    
    int main(void)
    {
       myFunction();
       return 0;
    }     
    
    Cause:
    This time, the compiler is happy. It compiles properly. It can see what 'myFunction' looks like, and it knows that it is being called properly in main.
    The linker, however, is not happy. Any time that you get errors that start with /var/tmp/whatever.o: this means that the linker is giving you the error. It is telling you that myFunction(void) is undefined. It can't find a function definition for myFunction that matches the way that you are invoking it.

    Solution:
    Often this is caused by not including all of the cpp files on the g++ line when you attempt to compile/link. If you do have all of the cpp files necessary, make sure that the prototype for 'myFunction' EXACTLY matches the first line of the function definition (except for the semicolon, of course). If there is anything different that could cause problems. For instance, you might have a function definition for myFunction, but it might start like:

    void myFunction(const short knNum)
    {
    
    You will still get this error. The function definition needs to match the prototype.
    If you are wanting to test your program before you actually write the entire code for 'myFunction' you will need to create a function stub for myFunction. It doesn't have to do much, but it does have to exist. For the above example, a perfectly good function stub for myFunction might be:
    void myFunction()
    {
       cout << "inside myFunction()" << endl;
       return;
    }
    

  24. Error:
    cppfile.cpp: In function `int main()':
    cppfile.cpp:3: too few arguments to function `void myFunction(short int)'
    cppfile.cpp:7: at this point in file     
    
    
    or
    
    cppfile.cpp: In function `int main()':
    cppfile.cpp:3: too many arguments to function `myFunction()'
    cppfile.cpp:7: at this point in file                           
    
    Pertinent Code:
    #include <iostream>                      1
                                               2
    void myFunction(const short knNum);        3
                                               4
    int main(void)                             5
    {                                          6
       myFunction();                           7
       return 0;
    }  
    
    
    or
    
    
    
    #include <iostream>                       1
                                               2
    void myFunction();                         3
                                               4
    int main(void)                             5
    {                                          6
       myFunction(12);                         7
       return 0;
    }     
    
    Cause:
    myFunction() is being called incorrectly. According to the prototype myFunction should have a short integer inside the parenthesis (or not, in the second case). In this example the invokation doesn't match that.

    Solution:
    Make sure that the function invokation matches the prototype. Note: It does NOT matter if the actual code for the function starts with:

    void myFunction()
    {
    
    
    
    or (in the second case)
    
    
    
    void myFunction(const short knNum)
    {
    
    When the compiler tries to compile cppfile.cpp it only looks at the prototype, and the line in main does not match the prototype.

  25. Error:
    /var/tmp/ccQMExFq.o: In function `myFunction(void)':
    /var/tmp/ccQMExFq.o(.text+0x0): multiple definition of `myFunction(void)'
    /var/tmp/ccTymKYW.o(.text+0x0): first defined here
    /afs/umr.edu/software/egcs11/solaris/sparc-sun-solaris2.5.1/bin/ld: Warning: size of symbol `myFunction(void)' changed from 20 to 36 in /var/tmp/ccQMExFq.o
    collect2: ld returned 1 exit status                        
    
    Pertinent Code:
    in cppfile.cpp:
    
    
    #include "myfunction.cpp"         2
                                      3
    int main(void)                    4
    {                                 5
       myFunction();                  6
    
    
    and in myfunction.cpp:
    
    void myFunction()                 1
    {                                 2
       return;                        3
    }     
    
    Cause:
    This is a linker error. It is seeing two function definitions for myFunction. This has been compiled with:
    g++ -Wall -W -s cppfile.cpp myfunction.cpp
    
    This is the proper command line to use to compile. However the code isn't correct. Remember what a #include does. It tells the preprocessor to take the code in that file and insert it. So, after the preprocessor gets done with cppfile.cpp, we are basically left with:
    void myFunction()                 
    {                                 
       return;                        
    }   
    
    int main(void)                    
    {                                 
       myFunction();                  
    
    This compiles fine. main can see what myFunction looks like, it is happy. This will compile into object code for two functions: main and myFunction.

    Then, we compile myfunction.cpp. This will compile into object code for one function: myFunction. When the linker attempts to link everything together, it discovers that it has two sets of object code for myFunction.

    Solution:
    DON'T include cpp files! Never ever ever ever ever #include a cpp file. Only header files get #include'd.

  26. Error:
    /var/tmp/ccVqedYJ.o: In function `main':
    /var/tmp/ccVqedYJ.o(.text+0x0): multiple definition of `main'
    /var/tmp/ccwtOuE9.o(.text+0x0): first defined here
    collect2: ld returned 1 exit status              
    
    Pertinent Code:
    Compilation line:
    
    g++ -Wall -W -s *.cpp -o executableName
    
    Cause:
    There are some extra cpp files in the current directory that have a main function defined. So, you are getting the main() that you want, but you are also getting the other main() functions.

    Solution:
    If you are going to use the *.cpp method to compile make sure that you don't have ANY other cpp files in the current directory.