简体   繁体   English

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

[英]C++ operator overloading in a derived class

I have a template class matrix and I try to override the + operation for different matrix. 我有一个模板类matrix ,我尝试覆盖不同矩阵的+操作。 I want to override the operator in a derived class but have a few errors. 我想在派生类中重写运算符,但有一些错误。 In other words, I want to be able to do only A+B for 2 defined objects of matrix and not to to do a for loop. 换句话说,我只希望对matrix 2个定义的对象执行A+B ,而不希望进行for循环。 Here is the code: 这是代码:

#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;
}

The class matrix works fine. matrix工作正常。 It is when I added the Operations class that I got the following erros: 当我添加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)

Can you please explain me the reason for those errors and how to correct them? 您能为我解释这些错误的原因以及如何纠正它们吗? Thank you 谢谢

EDIT I'd like to implement the + operator as a member class function in the matrix , but I am getting an error message: Can you please tell me what is going wrong with the way I define operator+? 编辑我想将+运算符实现为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+() is normally expected to either be a member of the class it acts on (and accept one argument) or be a non-member that accepts two arguments. 通常期望operator+()是它所作用的类的成员(并接受一个参数),或者是接受两个参数的非成员。

For example; 例如;

class matrix
{
    public:

        matrix operator+(const matrix &) const;   // member version
};

or 要么

class matrix
{
    public:

        friend matrix operator+(const matrix &, const matrix &);
};

matrix operator+(const matrix &, const matrix &);

Note that friendship is optional in the second case, if the matrix provides public member functions that the operator+() needs. 请注意,如果矩阵提供了operator+()所需的公共成员函数,则在第二种情况下,友谊是可选的。

The two approaches cannot be mixed. 两种方法不能混合使用。 If the compiler encounters a class with both forms of operator+() , it has no basis to prefer calling one over the other, and will reject code like c = a+b due to ambiguity. 如果编译器遇到同时具有两种形式的operator+() ,则它没有理由偏向于调用另一种形式,并且由于歧义性而将拒绝c = a+b类的代码。

It is not also possible for another class (like your Operations ) to provide a non-static member function operator+() that accepts two arguments of type matrix . 另一个类(例如您的Operations )也不可能提供一个接受两个类型为matrix参数的非静态成员函数operator+() The error message you are getting about a "non-function" is referring to that (albeit the compiler vendor has used rather cryptic wording for the error message). 您收到的有关“非功能”的错误消息是指该错误消息(尽管编译器供应商已对该错误消息使用了相当晦涩的措辞)。

Also note that operator+() should generally return by value, rather than returning a reference. 还要注意, operator+()通常应按值返回,而不是返回引用。 Returning a reference usually causes expression like c = a+b to work differently than expected - and (depending on what reference is returned) can cause the caller to exhibit undefined behaviour. 返回引用通常会使c = a+b类的表达式工作与预期不同-并且(取决于所返回的引用)可能导致调用方表现出不确定的行为。

I think you're just supposed to overload the + operator for the matrix class 我认为您只应该为matrix类重载+运算符

matrix& operator+(matrix& other){
   ....
}

You can therefor use it like : 为此,您可以像这样使用它:

matrix<type> a;
// initialise a
matrix<type> b;
// initialise b
matrix<type> c = a+b; // a+b <=> a.operator+(b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM