C++のlinkerエラーについて
現在C++のクラスの勉強を行なっています。
そこで挿入子の多重定義を行なったファイルのコンパイルを行なったところ、エラーメッセージが発生しました。
開発環境はMacで、ターミナルで実行しています。
###コンパイルは
発生している問題・エラーメッセージ
$ g++ -c Test1.cpp $ g++ -c Test2.cpp $ g++ -o Test1.o Test2.o Undefined symbols for architecture x86_64: "Test::Test()", referenced from: _main in Test2.o "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Test const&)", referenced from: _main in Test2.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
該当のソースコード
TestHed.h
C++
1#include <string> 2#include <iostream> 3 4class Test { 5 int y; 6 int m; 7 int d; 8 9public: 10 Test(); 11 std::string to_string() const; 12}; 13 14std::ostream& operator<<(std::ostream& s, const Test& x); 15
Test1.cpp
C++
1#include <sstream> 2#include <iostream> 3#include "TestHed.h" 4 5Test::Test() 6{ 7 y = 3; 8 m = 2; 9 d = 1; 10} 11 12std::string Test::to_string() const 13{ 14 std::ostringstream s; 15 s << y << "年" << m << "月" << d << "日"; 16 return s.str(); 17} 18
Test2.cpp
C++
1#include <iostream> 2#include "TestHed.h" 3 4int main() 5{ 6 Test test; 7 std::cout << test << '\n'; 8} 9
試したこと
ヘッダーファイルへのリンクがうまくできないと思い、ヘッダーファイルの置いてある場所も確認しましたが、同一のフォルダ内にありました。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/02 13:36