派生类中的C++运算符重载

C++ operator overloading in a derived class

本文关键字:运算符 重载 C++ 派生      更新时间:2023-10-16

我有一个模板类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);