template <class T>
class Matrix{//矩阵
friend istream& operator>>(istream&, Matrix<T>&);
friend ostream& operator<<(ostream&, Matrix<T>&);
public:
Matrix(){mRow=0, mCol=0, nZeroNum=0;}
private:
vector<Term<T>> vec;
int mRow,mCol;//矩阵的行数,列数
int nZeroNum;//矩阵中的非零元素个数
};
报错:
test21.obj : error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Matrix<int> &)" (
??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Matrix@H@@@Z)
test21.obj : error LNK2001: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Matrix<int> &)" (
??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Matrix@H@@@Z)
D:\DemoProject\test21\Debug\test21.exe : fatal error LNK1120: 2 unresolved externals
纠正如下:
template <class T>
class Matrix{//矩阵
template<class T>
friend istream& operator>>(istream&, Matrix<T>&);
template<class T>
friend ostream& operator<<(ostream&, Matrix<T>&);
public:
Matrix(){mRow=0, mCol=0, nZeroNum=0;}
private:
vector<Term<T>> vec;
int mRow,mCol;//矩阵的行数,列数
int nZeroNum;//矩阵中的非零元素个数
};