使用C++中的模板和运算符重载执行矩阵运算
Perform Matrix Operations using templates and operator overloading in C++
这是我的完整代码,其中我使用模板和运算符重载对矩阵执行了加法、减法和转置运算。当我使用成员函数时,一切都很好。但是,当我尝试使用下面所做的朋友功能时,会显示错误(稍后会提到(。有人能帮我纠正这个代码吗。
#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>
时,友元声明绑定到int
和float
。然后,按照定义
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>);
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 继承函数的重载解析
- C++,系统无法执行指定的程序
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 使用重载运算符在函数调用时执行操作
- 正确执行运算符重载和多态性
- 如何重载iostream运算符以在整个流上执行功能
- 对重载运算符参数执行隐式转换时出现编译器错误
- 无法弄清楚为什么使用重载比较运算符在执行前调用复制构造函数
- 赋值重载显式调用析构函数(在执行其他二进制重载运算符之后)
- 执行重载构造函数的原因和时间
- 使用类型谓词执行重载解析
- 为什么操作符重载不能正常执行
- 重载<<和>>C++以执行小数移位
- 如何在c++中执行操作符重载以避免if条件
- 重载函数和可执行文件/库的大小
- 构造函数和重载运算符执行顺序