いつもありがとうございます。
現在C++のリンカエラーで困っています。
具体的には以下のようにテンプレートを構造体に対して使用し、ヘッダファイルに定義を記述してプログラムを記述していると起こりました。
lang
1// header1.h 2#ifndef _H1_ 3#define _H1_ 4template<typename Type> 5struct 2D_ 6{ 7 Type x; 8 Type y; 9}; 10typedef 2D_<float> Real2D; 11typedef 2D_<int> 2D; 12#endif
lang
1// header2.h 2#ifndef _H2_ 3#define _H2_ 4#include "header1.h" 5unsigned char t(2D a, 2D b, int c) 6{ 7 ... 8} 9#endif
lang
1// main.cpp 2#include "header2.h" 3int main() 4{ 52D a; 62D b; 7a.x = 10; 8a.y = 20; 9b.x = 30; 10b.y = 40; 11t(a, b, 100); 12return 0; 13}
エラーメッセージは以下のようになっていました。
「_main.cpp.obj:error LNK2005:struct 2D<int>は既に定義されています。__」
どなたかテンプレートと構造体の連携に関してアドバイスを頂けたらと思います。
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2014/12/31 11:27
2016/10/08 08:28