Haskei 2016-09-08 09:25 采纳率: 0%
浏览 1024

C++ 运算符重载问题,求大神们帮忙,一直找不到解决办法

#include
#include
using namespace std;

template class TripleList;
template
class Triple
{
private:
int row,col;
T val;
public:
friend class TripleList;
friend istream& operator >> (istream& i,TripleList &M);
friend ostream& operator << (ostream& o,TripleList &M);
Triple(){}
Triple(int r, int c, T v):row(r),col(c),val(v){}
};

template
class TripleList
{
private:
Triple *data;
int rows,cols,nums;//nums表示非零元素的个数
int maxSize;
public:
TripleList(int size=40);
~TripleList(){delete[] data;}
bool Find(Triple value);
bool Insert(Triple value);
friend istream& operator >> (istream& i, TripleList &M);
friend ostream& operator << (ostream& o,TripleList &M);
bool Add(TripleList &M, TripleList &result);
bool Multiply(TripleList &M, TripleList &result);
void Transpose(TripleList &M);//矩阵转置
void FastTranspose(TripleList &M);
};

template
TripleList::TripleList(int size)
{
nums=0; maxSize=size; data=new Triple[maxSize];

}

template
bool TripleList::Insert( Triple val)//const
{
if(nums<=maxSize)
{
cout<<"fulled!"< return false;
}
if(val.row>rows || val.col>cols || val.row<0 || val.row<0)
{
cout<<"行标或列标越界!"<<endl;
return false;

}
data[nums++]=val;
return true;

}

template
bool TripleList::Find(Triple value)
{
for(int i=0;i<nums;i++)
{
if(data[i]==value)
{
cout<<"OK"<<endl;
return true;
}
}
cout<<"can not find!"<<endl;
return false;
}

template
istream& operator >> (istream &i, TripleList &M)
{
int j=0;
cout<<"输入矩阵的行数 ";
i>>M.rows;
cout<<"输入矩阵的列数 ";
i>>M.cols;
cout<<"输入矩阵的非零元素的个数 ";
i>>M.nums;
cout<<"输入"< while(i {
i>>M.data[j].row>>M.data[j].col>>M.data[j].val;
if(M.data[j].row>=M.rows || M.data[j].col>=M.cols || M.data[j].row<0 || M.data[j].col<0)
{
cout<<" 行标或列标越界,请重新输入 "<<endl;
continue;
}
j++;
}
return i;

}

template
ostream& operator << (ostream &o, TripleList &M)
{
o<<"这是一个"<<M.rows<<"X"<<M.cols<<" 的稀疏矩阵,它共有"<<M.nums<<"个非零元素!"<<endl;
o<<setw(10)<<"行数"<<setw(10)<<"列数"<<setw(10)<<"值"<<endl;
for(int i=0;i<M.nums;i++)
{
o<<M.data[i].row<<setw(10)<<M.data[i].col<<setw(10)<<M.data[i].val<<endl;
}
return o;
}

template
void TripleList::Transpose(TripleList &M)//矩阵的转置
{
if(!nums){return;}
M.data=new Triple[nums];
M.rows=cols,M.cols=rows;
for(int i=0;i<nums;i++)
{
M.data[i].row=data[i].col;
M.data[i].col=data[i].row;
M.data[i].val=data[i].val;
}
}

int main()
{
TripleList T,Arr;
cin>>T;
cout<<T;
T.Transpose(Arr);
cout<<Arr;
return 0;
}


Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream >&, TripleList&)", referenced from:
main in main.o
"operator>>(std::
_1::basic_istream >&, TripleList&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

  • 写回答

1条回答

  • HappyerMano 2016-09-10 06:05
    关注

    你这个应该是basic_ostream类里面的<<和>>操作符要重载

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题