所以我有这个非常基本的 OOP 示例,我想在 Xubuntu 中编译它,但我得到了错误
CThermo.h 文件
class CThermo
{
public:
void SetTemp(int newTemp);
int ReturnTemp();
void ChangeTemp(int deltaTemp);
private:
int m_temp;
};
CThermo.cpp 文件
#incude "CThermo.h"
void CThermo::SetTemp(int newTemp)
{
m_temp = newTemp;
}
int CThermo::ReturnTemp()
{
return m_temp;
}
void CThermo::ChangeTemp(int deltaTemp)
{
m_temp += deltaTemp;
}
main.cpp 文件
#include "CThermo.h"
#include <iostream>
using std::cout;
int main()
{
CThermo roomTemp;
roomTemp.SetTemp(20);
cout << "the temp is : "<< roomTemp.ReturnTemp() << "\n";
roomTemp.ChangeTemp(5);
cout << "after changing the temp, the room temp is : " << roomTemp.ReturnTemp();
cout << "test";
return 0;
}
编译的命令是“g++ main.cpp -o Main”,这是我得到的错误
/tmp/ccXajxEY.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `CThermo::SetTemp(int)'
main.cpp:(.text+0x26): undefined reference to `CThermo::ReturnTemp()'
main.cpp:(.text+0x6c): undefined reference to `CThermo::ChangeTemp(int)'
main.cpp:(.text+0x78): undefined reference to `CThermo::ReturnTemp()'
collect2: error: ld returned 1 exit status