LNK2019:模板类实现稀疏矩阵时重载<<和>>中的错误

QuestionMark_zhang 2006-03-25 11:00:02
程序大致如下:
#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 中被引用


达人们指点一下
不胜感激
...全文
126 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
QuestionMark_zhang 2006-03-26
  • 打赏
  • 举报
回复
bluejugar(2046,那年我64.):
我在Dev-CPP(4.9.9.0)下面编译了一下,通过,运行没问题.(加了int main(){return 0;})
__________________________________________________________

加上这样的就错了:
int main()
{
SparseMatrix<int> s;
cin >> s;
return 0;
}
bluejugar 2006-03-26
  • 打赏
  • 举报
回复
我在Dev-CPP(4.9.9.0)下面编译了一下,通过,运行没问题.(加了int main(){return 0;})
strangerryf 2006-03-26
  • 打赏
  • 举报
回复
本来想贴部分,看来补贴全是不行的。太长了,注意:function template declaration must precede friend declaration in class template definition。还有,<<和>>操作符须是Term的友元。参见c++ primer 4ed, chapter 16, 16.4. Class Template Members及附带的代码。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <iostream>
using namespace std;

template<class T> class SparseMatrix;

template <class T>
ostream& operator<<(ostream&, const SparseMatrix<T>&);

template <class T>
istream& operator>>(istream&, SparseMatrix<T>&);

template<class T>
class Term
{
friend class SparseMatrix<T>;
friend ostream& operator<< <T>( ostream &out, const SparseMatrix<T> &x );
friend istream& operator>> <T>( istream &in, SparseMatrix<T> &x );
private:
int row, col;
T value;
};

template<class T>
class SparseMatrix
{
friend ostream& operator<< <T>( ostream &out, const SparseMatrix<T> &x );
friend istream& operator>> <T>( 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;
}

int main()
{
SparseMatrix<int> s;
cin >> s;
return 0;
}
+++++++++++++++++++++++++++++++++++++
vs2k5(32b), gcc3.4.4(mingw32)通过。

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧