UPDATE: C++ undefined reference
When I try to run the following C++ program: UPDAT开发者_运维百科E (Updated code since the past link had some errors): http://pastie.org/private/pdpfpzg5fk7iegnohebtq
I get the following:
UPDATE
The errors that arise now are as follows:
Any ideas on that?
Thanks.
You're not linking in GradeBook.o so you're getting an undefefined reference. Try
g++ GradeBookMain.cc GradeBook.cc -o GradeBookMain
You also have a typo "maximun" instead of "maximum" in GradeBook.h
You didn’t tell your compiler where to find the GradeBook
constructor definition (hence “undefined reference”). You need to pass all source files separately to the compiler, or create intermediate object files for all compilation units, and link them together.
Effectively, the easiest solution is this:
g++ GradeBookMain.cc GradeBook.cc -o GradeBookMain
To quote one of my favorite IRC bots: Undefined reference is a linker error. It's not a compile error. #includes don't help. You did not define the thing in the error message, you forgot to link the file that defines it, you forgot to link to the library that defines it, or, if it's a static library, you have the wrong order on the linker command line. Check which one.
C++ is case sensitive. So, for instance you can displayMessage, but what you define is DisplayMessage. Those are two distinct functions. You should change the definition of DisplayMessage to displayMessage, or when you call it call DisplayMessage not displayMessage
What your compiler is telling you, is that the GradeBook class is defined and everything is OK at the compiling stage, but when the time comes to link a complete executable program, it can't find the actual code for that class. And this is because you have compiled and linked only GradeBookMain.cc and not GradeBook.cc. You can compile and link them both at the same time like this:
g++ GradeBookMain.cc GradeBook.cc -o program
Or you can compile them separately and then link together:
g++ -c GradeBookMain.cc -o GradeBookMain.o
g++ -c GradeBook.cc -o GradeBook.o
g++ GradeBookMain.o GradeBook.o -o program
You need to also compile in GradeBook.cc.
At the moment the class itself is not being compiled or linked, and as such, the linker cannot find the GradeBook class - causing it to complain.
精彩评论