0
我對C++刷牙和運行這個錯誤:未定義的符號,定義在頭文件和實現文件級
Undefined symbols for architecture x86_64:
"GradeBook::getCourseName()", referenced from:
_main in gbDriver-a5418c.o
"GradeBook::GradeBook(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in gbDriver-a5418c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
這是我的驅動程序文件(命名爲gbDriver.cpp
):
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
GradeBook gradeBook1("CS101 Introduction to C++ Programming");
GradeBook gradeBook2("CS102 Data Structures in C++");
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl;
}
這裏是我的頭文件中,我定義的類(接口)(命名爲Gradebook.h
):
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook(string);
void setCourseName(string);
string getCourseName();
void displayMessage();
private:
string courseName;
};
下面是我實現的GradeBook類的成員函數(名爲GradeBook.cpp
)文件:我在嘗試通過運行編譯運行到上述錯誤
#include <iostream>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook(string name)
{
setCourseName(name);
}
void GradeBook::setCourseName(string name)
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
}
:g++ gbDriver.cpp
。這是怎麼回事?
我知道可能是一件簡單的事情,但我無法弄清楚,這是讓我瘋狂。它似乎不需要我的編譯器,所以我猜我的代碼在某個地方是錯誤的。
感謝您的幫助。
顯示完全編譯命令...... –
感謝巴西萊。從包含所有三個文件的目錄:'g ++ gbDriver.cpp' – nacho
您必須列出所有需要編譯的'.cpp'文件。 – kfsone