繁体   English   中英

g ++编译错误

[英]g++ compilation error

所以我有这个非常基本的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

您必须使用以下命令编译main.cpp和CThermo.cpp:

g++ CThermo.cpp main.cpp -o Main

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM