2015-11-10 51 views
0

我有一個模板類matrix,我嘗試覆蓋對不同矩陣的+操作。我想覆蓋派生類中的運算符,但有一些錯誤。換句話說,我希望能夠爲matrix的2個已定義對象僅執行A+B,而不是執行for循環。 下面是代碼:C++運算符在派生類中重載

#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一個成員類的功能,但我得到的錯誤消息:你能告訴我什麼是與方式去錯了我定義operator +?

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; 
    } 
+0

請張貼一個簡單的例子。 – Columbo

+0

這不是運算符重載的工作原理。看到這裏的一些例子:http://en.cppreference。com/w/cpp/language/operators –

+1

爲什麼要讓'Operations'從'matrix'繼承?瘋了吧*! – YSC

回答

3

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),也不可能提供一個非靜態成員函數operator+(),該函數接受兩個類型爲matrix的參數。關於「無功能」的錯誤消息指的是(儘管編譯器供應商對錯誤消息使用了相當神祕的措詞)。

另請注意,operator+()通常應該按值返回,而不是返回參考。返回引用通常會導致像c = a+b這樣的表達式以不同於預期的方式工作 - 並且(取決於返回的引用)可能導致調用方顯示未定義的行爲。

+0

你還沒有提供任何可以證明其中一方的信息,所以要麼。只是不能一次。您還需要一個工作賦值操作符,拷貝構造函數等。 – Peter

+0

謝謝,我剛剛明白當你說可能只有一個操作符的定義時,你的意思。我選擇實現成員類的功能,但我得到一個錯誤。代碼在編輯問題。你能否請我在我定義成員類運營商的方式犯了一個錯誤? – George

+0

你的'operator +()'需要返回一個'matrix'的實例,而不是指向'T'的指針。 – Peter

0

我覺得你剛纔應該重載+運算符的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); 
+2

你覺得不對。 'operator +()'不應該返回一個引用,因爲它使像'a = a + b'這樣的表達式的行爲與調用者可能假設的行爲不同。 – Peter