Я новичок в C ++ и пытаюсь узнать, как вызывать класс из файла .h внешнего заголовка. Я просто помещаю testando.cpp
, teste.h
и teste.cpp
в один каталог. Эта проблема возникла, когда я попытался скомпилировать testando.cpp
:
/tmp/ccpF455q.o: In function `main'
testando.cpp:(.text+0x24): undefined reference to `teste::teste(int)'
testando.cpp:(.text+0x30): undefined reference to `teste::get()'
collect2: error: ld returned 1 exit status:
Код:
//teste.h
#ifndef TESTE_H
#define TESTE_H
class teste
{
private:
int x;
public:
teste(int p);
int get();
};
#endif
//teste.cpp
#include "teste.h"
teste::teste(int p)
{
x=p;
}
int teste::get()
{
return x;
}
//testando.cpp
#include<iostream>
using namespace std;
#include "teste.h"
int main()
{
teste k(17);
cout << k.get() << endl;
}
Do Мне нужно скомпилировать файл заголовков или сделать что-то еще, чего я не понял?