重複の可能性:
テンプレート クラスの実装と宣言を同じヘッダー ファイルに含める必要があるのはなぜですか?
テンプレート クラス メンバー関数の実装は、常に C++ のヘッダー ファイルに入れる必要がありますか?
ここで愚かな間違いを犯しましたが、何がわかりません。
これはコンパイルされます:
//start of header.h
namespace ns
{
class A
{
public:
template <class t>t func();
};
template<class t> t A::func()
{
t a;
return a;
}
}
//end of header.h
//imp.cpp の開始
//imp.cpp の終わり
ただし、次の場合はそうではありません。
//start of header.h
namespace ns
{
class A
{
public:
template <class t>t func();
};
}
//end of header.h
//start of imp.cpp
#include "header.h"
using namespace ns;
template<class t> t A::func()
{
t a;
return a;
}
//imp.cpp の終わり
エラーは次のとおりです: error LNK2019: unresolved external symbol "public: int __thiscall ns::A::func(void)" (??$func@H@A@ns@@QAEHXZ) referenced in function _main