派生类中的C++运算符重载
C++ operator overloading in a derived class
我有一个模板类matrix
,我试图覆盖不同矩阵的+
操作。我想重写派生类中的运算符,但有一些错误。换句话说,我希望能够仅对matrix
的2个定义对象执行A+B
,而不执行for
循环。这是代码:
#include<iostream>
using namespace std;
template<class T>
class matrix
{
protected:
T **tab;
int row,col;
public:
matrix(int row,int col)
{
this->row = row;
this->col = col;
tab = new T* [col];
for(int i = 0; i < col; i++)
tab[i] = new T[row];
tab[0][0] = 100;
}
T& operator()(int i,int j)
{
return tab[i][j];
}
T operator()(int i,int j)const
{
return tab[i][j];
}
void operator=(const matrix& that)
{
for(int i = 0; i < col ; i++)
for(int j = 0; j < row ; j++)
tab[i][j] = that.tab[i][j];
}
matrix(const T& tab)
{
row = tab.row;
col = tab.col;
tab = new T* [col];
for(int i = 0; i < col; i++)
tab[i] = new T[row];
}
~matrix()
{
for(int i = 0; i < col; i++)
delete tab[i];
delete tab;
}
};
template<class T>
class Operations: public matrix<T>
{
T& operator+(matrix& tab1,matrix& tab2)
{
int i,j;
ligne = tab.ligne;
col = tab.col;
tab3 = new T* [col];
for(int i = 0; i < col; i++)
tab3[i] = new T[ligne];
for(i = 0; i < col; i++)
for(j = 0; j < ligne; j++)
tab3[i][j] = tab1[i][j]+tab2[i][j];
return tab3;
}
};
int main()
{
matrix<int> tab1(10,10);
matrix<int> tab2(5,5);
cout<<tab1(0,0)<<endl;
tab1(0,0) = 10;
cout<<tab2(0,0)<<endl;
tab2(0,0) = 5;
tab2 = tab1;
tab1(0,0) = 3;
return 0;
}
matrix
类运行良好。当我添加Operations
类时,我得到了以下错误:
v2.cpp:65:15: error: declaration of ‘operator+’ as non-function
T& operator+(matrix& tab1,matrix& tab2)
^
v2.cpp:65:13: error: expected ‘;’ at end of member declaration
T& operator+(matrix& tab1,matrix& tab2)
^
v2.cpp:65:21: error: expected ‘)’ before ‘&’ token
T& operator+(matrix& tab1,matrix& tab2)
你能给我解释一下这些错误的原因以及如何纠正吗?谢谢
编辑我想在matrix
中将+运算符实现为成员类函数,但我收到一条错误消息:你能告诉我定义运算符+的方式出了什么问题吗?
matrix operator+(const matrix& that)const
{
int i,j;
T** tab3;
tab3 = new T* [col];
for(int i = 0; i < col; i++)
tab3[i] = new T[row];
for(i = 0; i < col; i++)
for(j = 0; j < row; j++)
tab3[i][j] = that.tab[i][j]+tab[i][j];
return tab3;
}
operator+()
通常应该是其作用的类的成员(并接受一个参数),或者是接受两个参数的非成员。
例如;
class matrix
{
public:
matrix operator+(const matrix &) const; // member version
};
或
class matrix
{
public:
friend matrix operator+(const matrix &, const matrix &);
};
matrix operator+(const matrix &, const matrix &);
注意,在第二种情况下,如果矩阵提供operator+()
所需的公共成员函数,则友谊是可选的。
这两种方法不能混为一谈。如果编译器遇到一个同时具有两种形式的operator+()
的类,它就没有理由更喜欢调用其中一种而不是另一种,并且会因为歧义而拒绝像c = a+b
这样的代码。
另一个类(如Operations
)也不可能提供接受两个类型为matrix
的参数的非静态成员函数operator+()
。您收到的关于"非函数"的错误消息指的是它(尽管编译器供应商对错误消息使用了相当隐晦的措辞)。
还要注意,operator+()
通常应该按值返回,而不是返回引用。返回引用通常会导致像c = a+b
这样的表达式与预期的工作方式不同,并且(取决于返回的引用)可能会导致调用方表现出未定义的行为。
我认为您应该为matrix
类重载+运算符
matrix& operator+(matrix& other){
....
}
因此,您可以像这样使用它:
matrix<type> a;
// initialise a
matrix<type> b;
// initialise b
matrix<type> c = a+b; // a+b <=> a.operator+(b);
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板