アプリケーションに一般的な問題があります。パフォーマンスを向上させるために、作成したArrayクラスで遅延初期化を使用したいと思います。すべてのコードが単一の*.hファイルにある場合はすべて正常に機能しますが、コードを* .h * .cppファイルに分割すると、コンパイラーはリンカーエラーを返します。ヘッダーファイルには、
template <class T>
class simpleArray { ... some methods\operators declarations. ... }
と
template <typename T, typename derType=simpleArray<T>>
class myArray{.... some methods\operators declarations. ... }
そして私は次の明示的な宣言をしました:
template class myArray<double>;
template class myArray<int>;
template class simpleArray<double>;
template class simpleArray<int>;
* .cppファイルには、メソッドと演算子の実装があります。特に、2つの割り当て演算子があります。
template <class T, class derType>
myArray<T,derType>& myArray<T,derType>::operator= (myArray<T,derType> const& right)
{... some code ...}
template<class T, class derType>
template <class T2, class R2>
myArray<T,derType>& myArray<T,derType>::operator= (myArray<T2,R2> const& right)
{ ... some code ...}
最初のものは正常に動作し、2番目のもの(Arra = Array)は次のエラーを返します。
Error 70 error LNK1120: 1 unresolved externals
Error 69 error LNK2019: unresolved external symbol "public: class myArray <double,class simpleArray<double> > & __thiscall myArray <double,class simpleArray<double> >::operator=<int,class simpleArray<int> >(class myArray <int,class simpleArray<int> > const &)"
いくつかの解決策を提案してもらえますか?すべてのコードを同じファイルに含める必要がありますか?はっきりしているといいのですが。サポートしてくれてありがとう!
追伸 テンプレートを使用する場合のコード編成に関する「ベストプラクティス」ドキュメントはありますか?