使用C++中的模板和运算符重载执行矩阵运算

Perform Matrix Operations using templates and operator overloading in C++

本文关键字:执行 重载 运算 运算符 C++ 使用      更新时间:2023-10-16

这是我的完整代码,其中我使用模板和运算符重载对矩阵执行了加法、减法和转置运算。当我使用成员函数时,一切都很好。但是,当我尝试使用下面所做的朋友功能时,会显示错误(稍后会提到(。有人能帮我纠正这个代码吗。

#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 matricesn2-Show matricesn3-Addn4-Subtractn5-Multiplyn6-Transposen7-ExitnPlease selct a choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"For integer matricesn";
cout<<"For the first matrix n";
m1.get();
cout<<"For the second matrix n";
m2.get();
cout<<"nFor float matricesn";
cout<<"For the first matrix n";
m4.get();
cout<<"For the second matrix n";
m5.get();
break;
case 2:
cout<<"For integer matricesn";
cout<<"First matrix n";
m1.show();
cout<<"Second matrix n";
m2.show();
cout<<"nFor float matricesn";
cout<<"First matrix n";
m4.show();
cout<<"Second matrix n";
m5.show();
break;
case 3:
cout<<"For integer matricesn";
if(m1.check(m2))
cout<<"Addition not possiblen";
else
{
cout<<"Additionn";
m3=m1+m2;
m3.show();
}
cout<<"nFor float matricesn";
if(m4.check(m5))
cout<<"Addition not possiblen";
else
{
cout<<"Additionn";
m6=m4+m5;
m6.show();
}
break;
case 4:
if(m1.check(m2))
cout<<"Subtraction not possiblen";
else
{
cout<<"Subtractionn";
m3=m1-m2;
m3.show();
}
cout<<"nFor float matricesn";
if(m4.check(m5))
cout<<"Subtraction not possiblen";
else
{
cout<<"Subtractionn";
m6=m4-m5;
m6.show();
}
break;
case 5:
cout<<"For integer matricesn";
cout<<"Multiplicationn";
m3=m1*m2;
m3.show();
cout<<"nFor float matricesn";
cout<<"Multiplicationn";
m6=m4*m5;
m6.show();
break;
case 6:
cout<<"For integer matricesn";
cout<<"Transpose of first matrixn";
m3=!m1;
m3.show();
cout<<"Transpose of second matrixn";
m3=!m2;
m3.show();
cout<<"nFor float matricesn";
cout<<"Transpose of first matrixn";
m6=!m4;
m6.show();
cout<<"Transpose of second matrixn";
m6=!m5;
m6.show();
break;
case 7:
break;
default:
cout<<"Please enter a valid choicen";
}
}while(ch!=7);
return 0;
}

现在,我在输出中得到了什么
警告:友元声明"mat operator+(mat,mat("声明了一个非模板函数[-Wnon-template友元]
注意:(如果这不是你想要的,请确保已经声明了函数模板,并在此处的函数名称后添加<>(
错误:对operator+的未定义引用
error:对operator+[mat,mate]的未定义参考

实例化mat<int>mat<float>时,友元声明绑定到intfloat。然后,按照定义

template<class T>
mat<T> operator+(mat<T> A,mat<T> B)
{
mat<T> C;
C.r=A.r; C.c=A.c;
...
}

与那些声明没有关系,并且您会得到一个链接器错误。

您必须将friend声明为函数模板。也就是说,我们应该重写以下行

friend mat<T> operator+(mat<T>, mat<T>);

如下所示:

template<class U> friend mat<U> operator+(mat<U>, mat<U>);