16,551
社区成员
发帖
与我相关
我的任务
分享
#include "iostream"
using namespace std;
template<class T>
class C1
{
friend ostream& operator<<(ostream&,const C1<T>&);
public:
C1(){value = 999;}
private:
int value;
};
//重载<<
template<class T>
ostream& operator<<(ostream& out,const C1<T>& c)
{
out << c.value;
return out;
}
//换成下面的函数能正常工作
//ostream& operator<<(ostream& out,const C1<int>& c) {out << c.value;return out;}
int main()
{
C1<int> c;
cout << c << endl;
return 0;
}
#include "iostream"
using namespace std;
template<class T>
class C1
{
template<class U>
friend ostream& operator<<(ostream&,const C1<U>&);
public:
C1(){value = 999;}
private:
int value;
};
//重载<<
template<class T>
ostream& operator<<(ostream& out,const C1<T>& c)
{
out << c.value;
return out;
}
int main()
{
C1<int> c;
cout << c << endl;
return 0;
}
template <class T>
ostream& operator <<(ostream& out,const T& c)
{
out << c.value;
return out;
}
template <class T>
class C1
{
friend ostream& operator << <C1<T>> (ostream&,const C1<T>&);
public:
C1(){value = 999;}
private:
int value;
};