LNK2019:模板类实现稀疏矩阵时重载<<和>>中的错误
程序大致如下:
#include <iostream>
using namespace std;
template<class T>
class SparseMatrix;
template<class T>
class Term
{
friend class SparseMatrix<T>;
private:
int row, col;
T value;
};
template<class T>
class SparseMatrix
{
friend ostream& operator<<( ostream &out, const SparseMatrix<T> &x );
friend istream& operator>>( istream &in, SparseMatrix<T> &x );
public:
SparseMatrix( int maxTerms = 10 );
~SparseMatrix() { delete []a; }
private:
int rows, cols;
int terms; //非零元素数目
Term<T> *a;
int MaxTerms;
};
template<class T>
SparseMatrix<T>::SparseMatrix( int maxTerms /* = 10 */ )
{
MaxTerms = maxTerms;
a = new Term<T>[MaxTerms];
terms = rows = cols = 0;
}
template<class T>
ostream& operator<<( ostream &out, const SparseMatrix<T> &x )
{
out << "rows = " << x.rows << " columns = " << x.cols << endl;
out << "nonzero terms = " << x.terms << endl;
for ( int i = 0; i < x.terms; i++ )
out << "a( " << x.a[i].row << ", " << x.a[i].col << " ) = " << x.a[i].value << endl;
return out;
}
template<class T>
istream& operator>>( istream &in, SparseMatrix<T> &x )
{
cout << "Enter numbers of rows, columns, and terms " << endl;
in >> x.rows >> x.cols >>x.terms;
//if ( x.terms > x.MaxTerms ) throw Nomem();
for ( int i = 0; i < x.terms; i++ ) {
cout << "Enter row, column, and value of term " << ( i + 1 ) << endl;
in >> x.a[i].row >> x.a[i].col >> x.a[i].value;
}
return in;
}
VS.net2003环境,模版类和模版函数都在同一个.h文件中
编译时出现下面错误error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class SparseMatrix<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$SparseMatrix@H@@@Z) ,该符号在函数 _main 中被引用
达人们指点一下
不胜感激