3 つのファイルで構成される次の C++ プログラムを作成しました。
thing.h ファイル
#ifndef THING_H
#define THING_H
class thing{
double something;
public:
thing(double);
~thing();
double getthing();
void setthing(double);
void print();
};
#endif
thing.cpp ファイル
#include <iostream>
#include "thing.h"
thing::thing(double d){
something=d;
}
thing::~thing(){
std::cout << "Destructed" << std::endl;
}
double thing::getthing(){
return something;
}
void thing::setthing(double d){
something = d;
}
void thing::print(){
std::cout <<"The someting is " << something << std::endl;
}
メインファイル
#include <iostream>
#include "thing.h"
int main(){
thing t1(5.5);
t1.print();
t1.setthing(7.);
double d=t1.getthing();
std::cout << d << std::endl;
system("pause");
return 0;
}
以前にこのプログラムをすべて 1 つのファイルで作成し、完全に実行しましたが、別のファイルに分割してヘッダーを作成しようとすると、リンカー エラーが発生します。メイン ファイルから実行しようとすると、次のエラーが発生します。
[Linker error] undefined reference to `thing::thing(double)'
[Linker error] undefined reference to `thing::print()'
[Linker error] undefined reference to `thing::setthing(double)'
[Linker error] undefined reference to `thing::getthing()'
[Linker error] undefined reference to `thing::~thing()'
[Linker error] undefined reference to `thing::~thing()'
ld returned 1 exit status
上記のエラーから、メイン ファイルがヘッダー内の関数を認識していないように見えます。これを修正するにはどうすればよいですか?