当我尝试运行以下 C++ 程序时:更新(更新的代码,因为过去的链接有一些错误): http: //pastie.org/private/pdpfpzg5fk7iegnohebtq
我得到以下信息:
更新
现在出现的错误如下:
有什么想法吗?
谢谢。
当我尝试运行以下 C++ 程序时:更新(更新的代码,因为过去的链接有一些错误): http: //pastie.org/private/pdpfpzg5fk7iegnohebtq
我得到以下信息:
更新
现在出现的错误如下:
有什么想法吗?
谢谢。
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
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
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
您的编译器告诉您的是,GradeBook 类已定义并且在编译阶段一切正常,但是当需要链接完整的可执行程序时,它无法找到该类的实际代码。这是因为您只编译和链接了 GradeBookMain.cc 而不是 GradeBook.cc。您可以像这样同时编译和链接它们:
g++ GradeBookMain.cc GradeBook.cc -o program
或者您可以单独编译它们然后链接在一起:
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.