我是 C++ 的初学者。我写了以下内容:
// GradeBook.h
#include <iostream>
#include <string>
using namespace std;
class GradeBook {
public:
GradeBook(string); // constructor that initializes courseName
void setCourseName(string); // function that sets the course name
string getCourseName(); // function that gets the course name
void displayMessage(); // function that displays a welcome message
private:
string courseName; // course name for this 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;
}
// main.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;
}
我正在使用 KDevelop 4.4.1,然后我继续执行我的 main.cpp,我得到了:
/home/brallan/projects/Hola/build> 使
链接 CXX 可执行 hola
CMakeFiles/hola.dir/main.o:在函数 main': /home/brallan/projects/Hola/main.cpp:
8:未定义对 GradeBook 的引用::GradeBook(std::string)'
/home/brallan/projects/Hola/main.cpp:9: 未定义对 GradeBook::GradeBook(std::string)'
/home/brallan/projects/Hola/main 的引用。 cpp:12: 未定义对 GradeBook::getCourseName()'
/home/brallan/projects/Hola/main.cpp:11: 未定义引用 GradeBook::getCourseName()'
collect2: error: ld 返回 1 个退出状态
make[ 2]: [hola] 错误 1
make[1]: [CMakeFiles/hola.dir/all] 错误 2
make: [all] 错误 2
失败
如果我从 Eclipse Juno CDT 运行相同的代码,它会返回:
gradeBook1 created for course: CS101 Introduction to C++ Programming
gradeBook2 created for course: CS102 Data Structures in C++
谁能帮我从 KDevelop 运行它?
更新:根据评论,KDevelop 没有编译项目中的其他文件:s 我想这是要解决的问题。