要像你一樣引用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>
將整個模板聲明爲朋友,而不僅僅是需要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版本。 – 2012-07-30 04:37:35