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个回答

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问