为了练习刚刚学的模板,就把以前写的一个矩阵的代码拿来试验,结果发现并不是那么容易,特别是当,函数不是类的成员函数时,就出了问题。类中定义友元friend ostream& operator<<(ostream&,const matrix&);
而函数定义为template<typename T>
ostream& operator<<(ostream& os,const matrix<T>& rhs)
{
for(int ix=0;ix!=4;++ix)
{
for(int iy=0;iy!=4;++iy)
os<<rhs.array1[ix][iy]<<" ";
os<<endl;
}
return os;
结果仍然报错,实在不明白是为什么,然后在网上查出,将friend ostream& operator<<(ostream&,const matrix&);改为friend ostream& operator<< <>(ostream&,const matrix&);就行了,虽然还是不明白为什么,不过觉得自己还是蛮有收获的,不管学了什么,不实际操练起来,你就能不会发现其中的一些隐藏的要点!看似简单的东西,只有真正的试验后你才能真切的感受到它的奥秘!