對不起,如果這個問題已被回答allready,但我有一個閱讀了很多問題考慮重載這個運算符在模板類,但我還沒有找到像我這樣的特殊情況。重載運算符<<在模板類
這是我的代碼:
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template <typename T>
class Class
{
T x;
public:
friend ostream& operator << (const Class<T>&, ostream& out);
};
template <typename T>
ostream& operator << (const Class<T>&, ostream& out)
{
return (out << out.x << endl);
}
int main()
{
Class<short> object;
cout << object << endl;
}
而且我得到的最後一行此錯誤: 錯誤1錯誤C2679:二進制「< <」:沒有操作員發現這需要類型的右手操作數「類(或沒有可接受的轉換)G:\ ucenje \ objektno orijentirano programiranje \ template3 \ template3 \ main.cpp中25 1 template3
的可能的複製[朋友聲明聲明一個非模板函數(http://stackoverflow.com/questions/4039817/friend-declaration-declares-a-non-template-function ) –