【发布时间】:2018-11-03 13:34:54
【问题描述】:
这是我使用模板和运算符重载对矩阵执行加法、减法和转置操作的完整代码。 当我使用会员功能时,一切都很好。但是当我尝试使用下面的朋友功能时,会显示错误(稍后提到)。 有人可以帮我更正这段代码吗?
#include<iostream>
using namespace std;
template<class T>
class mat
{
int r,c;
T m[5][5];
public:
mat(){}
int check(mat);
void get();
void show();
friend mat<T> operator+(mat<T>, mat<T>);
mat<T> operator-(mat<T>);
mat<T> operator*(mat<T>);
mat<T> operator!();
};
template<class T>
int mat<T>::check(mat<T> B)
{
if(r==B.r && c==B.c)
return 0;
return -1;
}
template<class T>
void mat<T>::get()
{
cout<<"Enter the no of rows and columns ";
cin>>r>>c;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
cout<<"Enter element a"<<i<<j<<" : ";
cin>>m[i][j];
}
}
template<class T>
void mat<T>::show()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cout<<m[i][j]<<"\t";
cout<<endl;
}
}
template<class T>
mat<T> operator+(mat<T> A,mat<T> B)
{
mat<T> C;
C.r=A.r; C.c=A.c;
for(int i=0;i<A.r;i++)
for(int j=0;j<A.c;j++)
C.m[i][j]=A.m[i][j]+B.m[i][j];
return C;
}
template<class T>
mat<T> mat<T>::operator-(mat<T> B)
{
mat C;
C.r=r; C.c=c;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
C.m[i][j]=m[i][j]-B.m[i][j];
return C;
}
template<class T>
mat<T> mat<T>::operator*(mat<T> B)
{
mat C;
C.r=r; C.c=B.c;
for(int i=0;i<r;i++)
{
for(int j=0;j<B.c;j++)
{
C.m[i][j]=0;
for(int k=0;k<c;k++)
{
C.m[i][j]+=(m[i][k]*B.m[k][j]);
}
}
}
return C;
}
template<class T>
mat<T> mat<T>::operator!()
{
mat C;
C.r=c; C.c=r;;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
C.m[j][i]=m[i][j];
return C;
}
int main()
{
int ch,r1,r2,c1,c2,ch1;
mat<int> m1,m2,m3;
mat<float> m4,m5,m6;
do
{
cout<<"\n***MENU***\n1-Enter matrices\n2-Show matrices\n3-Add\n4-Subtract\n5-Multiply\n6-Transpose\n7-Exit\nPlease selct a choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"For integer matrices\n";
cout<<"For the first matrix \n";
m1.get();
cout<<"For the second matrix \n";
m2.get();
cout<<"\nFor float matrices\n";
cout<<"For the first matrix \n";
m4.get();
cout<<"For the second matrix \n";
m5.get();
break;
case 2:
cout<<"For integer matrices\n";
cout<<"First matrix \n";
m1.show();
cout<<"Second matrix \n";
m2.show();
cout<<"\nFor float matrices\n";
cout<<"First matrix \n";
m4.show();
cout<<"Second matrix \n";
m5.show();
break;
case 3:
cout<<"For integer matrices\n";
if(m1.check(m2))
cout<<"Addition not possible\n";
else
{
cout<<"Addition\n";
m3=m1+m2;
m3.show();
}
cout<<"\nFor float matrices\n";
if(m4.check(m5))
cout<<"Addition not possible\n";
else
{
cout<<"Addition\n";
m6=m4+m5;
m6.show();
}
break;
case 4:
if(m1.check(m2))
cout<<"Subtraction not possible\n";
else
{
cout<<"Subtraction\n";
m3=m1-m2;
m3.show();
}
cout<<"\nFor float matrices\n";
if(m4.check(m5))
cout<<"Subtraction not possible\n";
else
{
cout<<"Subtraction\n";
m6=m4-m5;
m6.show();
}
break;
case 5:
cout<<"For integer matrices\n";
cout<<"Multiplication\n";
m3=m1*m2;
m3.show();
cout<<"\nFor float matrices\n";
cout<<"Multiplication\n";
m6=m4*m5;
m6.show();
break;
case 6:
cout<<"For integer matrices\n";
cout<<"Transpose of first matrix\n";
m3=!m1;
m3.show();
cout<<"Transpose of second matrix\n";
m3=!m2;
m3.show();
cout<<"\nFor float matrices\n";
cout<<"Transpose of first matrix\n";
m6=!m4;
m6.show();
cout<<"Transpose of second matrix\n";
m6=!m5;
m6.show();
break;
case 7:
break;
default:
cout<<"Please enter a valid choice\n";
}
}while(ch!=7);
return 0;
}
现在,我在输出中得到什么
警告:友元声明 'mat operator+(mat, mat)' 声明了一个非模板函数 [-Wnon-template-friend]
注意:(如果这不是您想要的,请确保已声明函数模板并在此处的函数名称后添加 )
错误:未定义对 operator+(mat, mat)的引用
错误:未定义对 operator+(mat, mat) 的引用
【问题讨论】:
-
欢迎来到 Stack Overflow!请使用tour 并阅读How to Ask。关于您的问题,不幸的是,它是题外话,因为此类问题必须附有minimal reproducible example。
标签: c++ templates operator-overloading friend