テンプレートの専門用語であるoperator<< <T, TSIZE>
のように、プライマリテンプレートの宣言を表示する必要があります。次にoperator<<
は、パラメータとして表示されるため、MyTest
の宣言が必要です。
// Declare MyTest because operator<< needs it
template<class T, unsigned int TSIZE> class MyTest;
// Declare primary template
template<class T, unsigned int TSIZE>
inline std::ostream& operator<<(std::ostream& lhs, const MyText<T, TSIZE>& rhs);
template<class T, unsigned int TSIZE> class MyTest
{
// Specialization MyTest<T, TSIZE> only declares
// specialization operator<< <T, TSIZE> as friend
// Note that you can just use '<>' to designate the specialization,
// template parameters are deduced from the argument list in this case
inline friend std::ostream& operator<< <> (std::ostream &lhs, const MyTest<T, TSIZE> &rhs);
};
これらの宣言と一致する必要があります。 operator<<
はテンプレートなので、その定義はヘッダ内にあるはずです。
これらのプリエンプティブな宣言をすべて記述するときに必要な作業が少なくて済むのは、MyTest<T, TSIZE>
の特殊化だけでなく、テンプレート全体をフレンドとして宣言することです。
// in MyTest definition
template<typename U, unsigned USIZE>
inline friend std::ostream& operator<<(std::ostream& lhs, const MyTest<U, USIZE>& rhs);
あなたが持っている定義はまた、このような宣言一致する必要があります(テンプレートパラメータの名前が一致する宣言と定義とは関係ありません)。
完全性のために、私は、クラステンプレートの友人になると、それをクラステンプレート定義で定義することを言及します。これは、各専門分野に固有の非テンプレート友人機能を定義します。
// in MyTest definition
friend std::ostream& operator<<(std::ostream& lhs, MyTest const& rhs)
{ /* implementation */ }
そのような関数を参照することは不可能である(例えば&ns::operator<<
が他のオプションとは異なり、動作しない)、これらは唯一ADLを介して発見されます。
"動作しません"という正確な意味...コンパイラエラー?どのエラー?最初の推測... "友人"は.cppバージョンに属していません。 –