求大神看看重载加号哪错了???

tianshuang.me 2014-04-13 05:31:37
其他代码都不用看,就看重载加号那,编译通不过,显示错误 1 error LNK2019: 无法解析的外部符号 "class TripleList<int> __cdecl operator+(class TripleList<int> const &,class TripleList<int> const &)" (??H@YA?AV?$TripleList@H@@ABV0@0@Z),该符号在函数 _main 中被引用 C:\Projects\稀疏矩阵\稀疏矩阵\源.obj 稀疏矩阵

附上代码:
头文件:
#ifndef TRIPLELIST_H
#define TRIPLELIST_H

template <class Type>
class TripleList;

template <class Type>
class Triple
{
friend class TripleList<Type>;
private:
int Row, Col;
Type Value;
public:
Triple() :Row(0), Col(0), Value(0){}
Triple(int R, int C, Type V) :Row(R), Col(C), Value(V){}
};

template <class Type>
class TripleList
{
public:
TripleList(int MaxSize = 100);
~TripleList(){ delete[]Array; }
void Insert();
int CompareTriple(const Triple<Type> &a, const Triple<Type> &b);
friend TripleList<Type> operator+(const TripleList<Type> &a, const TripleList<Type> &b);
TripleList<Type> *Transpose();
TripleList<Type> *FastTranspose();
void Display();
private:
int Rows, Cols, Terms;
Triple<Type> *Array;
int ArraySize;
};

template <class Type>
TripleList<Type>::TripleList(int Maxsize)
{
if (Maxsize < 1)
{
cout << "初始化错";
}
ArraySize = Maxsize;
Array = new Triple<Type>[Maxsize];
Terms = Rows = Cols = 0;
}

template <class Type>
void TripleList<Type>::Insert()
{
int R, C;
Type V;
cin >> R >> C >> V;
Triple<Type> p = Triple<Type>(R, C, V);
if (R>Rows)
Rows = R;
if (C>Cols)
Cols = C;
if (Terms == 0)
{
Array[Terms++] = p;
}
else
{
int pos = Terms;
for (int i = 0; i < Terms; ++i)
{
if (p.Row < Array[i].Row)
{
if (i<pos)
pos = i;
}
else if (p.Row == Array[i].Row)
{
if (p.Col < Array[i].Col)
if (i<pos)
pos = i;
}
}
if (pos < Terms)
{
for (int i = Terms - 1; i >= pos; --i)
Array[i + 1] = Array[i];
Array[pos] = p;
++Terms;
}
else
Array[Terms++] = p;
}
}

template <class Type>
int TripleList<Type>::CompareTriple(const Triple<Type> &a, const Triple<Type> &b)
{
if (a.Row == b.Row && a.Col == b.Col)
return 0;
else if (a.Row < b.Row || (a.Row == b.Row && a.Col < b.Col))
return -1;
else
return 1;
}

template <class Type>
TripleList<Type> operator+(const TripleList<Type> &a, const TripleList<Type> &b)
{
if (a.Rows != b.Rows || a.Cols != b.Cols)
throw domain_error("非同型矩阵不得相加");
TripleList<Type> c(a.Terms + b.Terms);
c.Rows = a.Rows;
c.Cols = a.Cols;
int i = 0, j = 0, k = 0;
while (i < a.Terms&&j < b.Terms)
{
switch (CompareTriple(a.Array[i], b.Array[j]))
{
case -1:
c.Array[k++] = a.Array[i++];
break;
case 1:
c.Array[k++] = b.Array[j++];
break;
default:
c.Array[k] = a.Array[i];
c.Array[k].Value += b.Array[j].Value;
if (c.Array[k].Value != 0)
++k;
++i;
++j;
break;
}
}
while (i<a.Terms)
c.Array[k++] = a.Array[i++];
while (j<b.Terms)
c.Array[k++] = b.Array[j++];
c.Terms = k;
return c;
}

template <class Type>
TripleList<Type> *TripleList<Type>::Transpose()
{
TripleList<Type> *b = new TripleList<Type>(ArraySize);
b->Rows = Cols;
b->Cols = Rows;
b->Terms = Terms;
if (Terms > 0)
{
int current = 0;
for (int col = 1; col <= Cols; ++col)
for (int p = 0; p < Terms; ++p)
if (Array[p].Col == col)
{
b->Array[current].Row = Array[p].Col;
b->Array[current].Col = Array[p].Row;
b->Array[current].Value = Array[p].Value;
++current;
}
}
return b;
}

template <class Type>
TripleList<Type> *TripleList<Type>::FastTranspose()
{
TripleList<Type> *b = new TripleList<Type>(ArraySize);
int *num = new int[Cols];
int *cpot = new int[Cols];
b->Rows = Cols;
b->Cols = Rows;
b->Terms = Terms;
int i;
if (Terms > 0)
{
for (i = 0; i < Cols; ++i)
num[i] = 0;
for (i = 0; i < Terms; ++i)
++num[Array[i].Col - 1];
cpot[0] = 0;
for (i = 1; i < Cols; ++i)
cpot[i] = cpot[i - 1] + num[i - 1];
for (int i = 0; i < Terms; ++i)
{
int current = cpot[Array[i].Col - 1];
b->Array[current].Row = Array[i].Col;
b->Array[current].Col = Array[i].Row;
b->Array[current].Value = Array[i].Value;
++cpot[Array[i].Col - 1];
}
}
delete[]num;
delete[]cpot;
return b;
}

template <class Type>
void TripleList<Type>::Display()
{
int p = 0;
for (int i = 1; i <= Rows; ++i)
{
for (int j = 1; j <= Cols; ++j)
{
if (i == Array[p].Row && j == Array[p].Col)
cout << Array[p++].Value << "\t";
else
cout << 0 << "\t";
}
cout << endl;
}
}

#endif


主函数:
#include <iostream>
#include "TripleList.h"
using namespace std;

int main()
{
int n;
cout << "建立矩阵A,请输入您要插入的非零元个数:";
cin >> n;
TripleList<int> A(n);
cout << "请按行数,列数,值的格式插入数据:"<<endl;
for (int i = 0; i < n; ++i)
{
A.Insert();
}
cout << "您输入的矩阵为:" << endl;
A.Display();
int m;
cout << "建立矩阵B,请输入您要插入的非零元个数:";
cin >> m;
TripleList<int> B(m);
cout << "请按行数,列数,值的格式插入数据:" << endl;
for (int i = 0; i < m; ++i)
{
B.Insert();
}
cout << "您输入的矩阵为:" << endl;
B.Display();
TripleList<int> C = A + B;
cout << "转置后的矩阵为:" << endl;
C.FastTranspose()->Display();
system("pause");
return 0;
}
...全文
167 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianshuang.me 2014-05-14
  • 打赏
  • 举报
回复
引用 3 楼 liu111qiang88 的回复:
http://www.360doc.com/content/10/0407/10/1072296_21913706.shtml 给你一个连接 看一下吧
就是要写在类中,写在外面不行
beautyangus 2014-04-14
  • 打赏
  • 举报
回复
哥哥,模板没有实例化啊! friend TripleList<Type> operator+(const TripleList<Type> &a, const TripleList<Type> &b); 只是引入operator+的友元,任何友元函数都需要友元声明时找到该函数的声明,友元声明时“绝不会”帮你实例化任何模板的,这个operator+是在后面声明的, TripleList<int> operator+(const TripleList<int> &a, const TripleList<int> &b); 这个函数在友元声明时还不存在,所以,在最终生成你的TripleList<int>时就出错了。 下面是可行的解决方案: 1.尽量少地重载全局+,这个重载应该作为成员函数来重载; 2.去掉这个友元声明; 3.定义实实在在的TripleList<int> operator+(const TripleList<int> &a, const TripleList<int> &b)出来。
火头军 2014-04-13
  • 打赏
  • 举报
回复
http://www.360doc.com/content/10/0407/10/1072296_21913706.shtml 给你一个连接 看一下吧
火头军 2014-04-13
  • 打赏
  • 举报
回复
模板类中的操作符重载 如果用友员连接会报错的,你用类成员的就可以啦
tianshuang.me 2014-04-13
  • 打赏
  • 举报
回复

65,208

社区成员

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

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