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):
cppfile.cpp: In function `int main()': cppfile.cpp:7: no match for `_IO_istream_withassign & << float &'Pertinent Code:
cin << fValue; 7Cause:
Solution:
Replace the <<
with >>
.
cppfile.cpp: In function `int main()': cppfile.cpp:6: no match for `_IO_ostream_withassign & >> char[7]'Pertinent Code:
cout >> "Hello!" >> endl; 6Cause:
Solution:
Replace the >>
with <<
.
cppfile.cpp: In function `int main()': cppfile.cpp:7: parse error before `>'Pertinent Code:
cout << "Hello!" << endl 6 cin >> fValue; 7Cause:
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.
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:
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'.
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; 7Cause:
Solution:
Make sure that the variable name given in the declaration exactly matches
its use in your C++ code.
cppfile.cpp: In function `int main()': cppfile.cpp:7: warning: converting `ostream & (*)(ostream &)' to `ios & (*)(ios &)' is a contravariance violationPertinent Code:
cin >> fValue >> endl; 7Cause:
endl
is only defined for use with cout
.
Solution:
Get rid of the >> endl
on the cin
line. ONLY
use endl
with cout
.
cppfile.cpp:10: undefined or invalid # directive cppfile.cpp: In function `int main()':Pertinent Code:
#icnlude <iostream> 10Cause:
Solution:
Spell include properly.
cppfile.cpp:3: person.h: No such file or directoryPertinent Code:
#include "person.h" 3Cause:
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.
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:
Solution:
Put in the semicolon (in the header file, after the closing curly
brace of the struct/class).
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 }: 8Cause:
Solution:
Change the colon to a semicolon.
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 12Cause:
Solution:
Spell ifndef correctly.
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 2Cause:
Solution:
Spell ifndef correctly.
cppfile.cpp:9: unterminated string or character constant cppfile.cpp:6: possible real start of unterminated constantPertinent Code:
float fValue; 5 cout << "Hello! << endl; 6 cin >> fvalue; 7 8 cout << "Goodbye!" << endl; 9Cause:
Solution:
Remember to always end literal strings with the closing quote.
cppfile.cpp: In function `int main()': cppfile.cpp:9: parse error before `<'Pertinent Code:
cin >> fValue; 7 8 cout << "fValue is" fValue << endl; 9Cause:
Solution:
Remember to seperate each "item" that you are printing with the insertion
operator.
cppfile.cpp:2: conio.h: No such file or directoryPertinent Code:
#include <conio.h> 2Cause:
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.
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; 8Cause:
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.)
In file included from cppfile.cpp:2: Person.h:1: unterminated `#if' conditionalPertinent 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 ifCause:
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.
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:
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".
cppfile.cpp: In function `int main()': cppfile.cpp:14: parse error at end of inputPertinent 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 } 13Cause:
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.
cppfile.cpp: In function `int main()': cppfile.cpp:12: parse error at end of inputPertinent 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 } 11Cause:
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.
cppfile.cpp: In function `int main()': cppfile.cpp:8: ANSI C++ forbids comparison between pointer and integerPertinent Code:
char cChoice; 5 6 cin >> cChoice; 7 if ("A" == cChoice) 8Cause:
"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'
.
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 statusPertinent Code:
#include <iostream> 1 2 int main(void) 3 { 4 myFunction(); 5 return 0; }Cause:
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.
/var/tmp/ccITB4j2.o: In function `main': /var/tmp/ccITB4j2.o(.text+0x4): undefined reference to `myFunction(void)' collect2: ld returned 1 exit statusPertinent Code:
#include <iostream> void myFunction(); int main(void) { myFunction(); return 0; }Cause:
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.
void myFunction() { cout << "inside myFunction()" << endl; return; }
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 filePertinent 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:
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.
/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 statusPertinent 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:
g++ -Wall -W -s cppfile.cpp myfunction.cppThis 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.
/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 statusPertinent Code:
Compilation line: g++ -Wall -W -s *.cpp -o executableNameCause:
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.