user727374
user727374

Reputation: 167

g++ compilation error

So i've got this very basic OOP example and i want to compile it in Xubuntu but I get errors

the CThermo.h file

class CThermo
{
public:
    void SetTemp(int newTemp);
    int ReturnTemp();
    void ChangeTemp(int deltaTemp);
private:
    int m_temp;
};

the CThermo.cpp file

#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;
}

the main.cpp file

#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;
}

the command to compile is "g++ main.cpp -o Main" and this are the errors I get

/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

Upvotes: 0

Views: 114

Answers (1)

Filip Vondr&#225;šek
Filip Vondr&#225;šek

Reputation: 1208

You have to compile both main.cpp and CThermo.cpp using:

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

Upvotes: 4

Related Questions