前提
言語:C++
OS:Linux
参考記事:http://www.slis.tsukuba.ac.jp/~fujisawa.makoto.fu/lecture/iml/cpp/cpp2.pdf
実現したいこと
C++で外部ファイルで定義したメソッドを呼び出したいです。
外部ファイルの.hと.cppそのメソッドは準備しましたが、ビルド時にエラーが発生してしまう為解決したいです。
発生している問題・エラーメッセージ
in function `main': undefined reference to `Foos::Foos()' undefined reference to `Foos::input(float, float, float)' undefined reference to `Foos::~Foos()' undefined reference to `Foos::~Foos()' collect2: error: ld returned 1 exit status
該当のソースコード
ビルドコマンドです
terminal
1g++ -o output main.cpp
main.cppファイルです
main.cpp
1Foos foosObj 2foosObj.input(1,2,3);
.hファイルです
test.h
1#ifndef FOOS_H_INCLUDED 2#define FOOS_H_INCLUDED 3#include <string> 4#include <iostream> 5#include <cstring> 6#include <stdio.h> 7using namespace std; 8 9class Foos 10{ 11private: 12 float x, y, z; 13 14public: 15 //コンストラクタとデストラクタ 16 Foos(); 17 Foos(float x0, float y0, float z0); 18 ~Foos(); 19 20public: 21 //入出力関数 22 void input(float x0, float y0, float z0); 23 void output(void); 24}; 25#endif
.cppファイルです
test.cpp
1#include <string> 2#include <iostream> 3#include <cstring> 4#include <stdio.h> 5#include <test.h> 6using namespace std; 7 8Foos::Foos() 9{ 10 cout << "countstructor" << endl; 11} 12Foos::Foos(float x0, float y0, float z0) 13{ 14x = x0; y = y0; z = z0; 15cout << "constructor2" << endl; 16} 17Foos::Foos() 18{ 19cout << "destructor" << endl; 20} 21void Foos::input(float x0, float y0, float z0) 22{ 23x = x0; y = y0; z = z0; 24} 25void Foos::output(void) 26{ 27cout << "v = (" << x << ", "; 28cout << y << ", "; 29cout << z << ")" << endl; 30}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/26 08:13