2013-05-04 29 views
0

這裏的,我有一個頭文件中創建一個類的摘錄:*這個 - >不能被用作函數

typedef double real; 

class Grid{ 
    public : 

     explicit Grid(); 
     explicit Grid(size_t level); 
     Grid(const Grid & grid); 

     ~ Grid(); 


     const Grid & operator =(const Grid & grid); 

     inline real & operator()(size_t i , size_t j); 
     inline real operator()(size_t i ,size_t j) const; 

     void fill(real value); 
     void setBoundary (real value); 

private : 

    size_t y_ ; // number of rows 
    size_t x_ ; // number of columns 
    real h_ ; // mesh size 
    real * v_ ; // values 
}; 

,這裏是從我已經爲先前宣佈編寫的代碼的摘錄函數在一個單獨的.cpp文件中。請注意,我只包含了與我的錯誤相關的部分。

inline real& Grid :: operator()(size_t i , size_t j){ 
    if((i >= y_) || (j>=x_)) 
     throw std::invalid_argument("Index out of bounds"); 
    return v_ [i* x_ +j]; 
    } 

    inline real Grid::operator()(size_t i ,size_t j) const{ 
    if((i >= y_) || (j>=x_)) 
     throw std::invalid_argument("Index out of bounds"); 
    return v_[i* x_+j]; 
    } 


    void Grid::fill(real value){ 
    for(size_t i=1;i<y_;++i){ 
    for(size_t j=1;j<x_;++j) 
    v_(i,j)=value; 
    } 

    } 

void Grid::setBoundary (real value){ 
    size_t i = 0; 
    for(size_t j=0;j<x_;++j){ 
    v_(i,j)=value; 
    } 
    i = y_; 
    for(size_t j=0;j<x_;++j){ 
    v_(i,j)=value; 
    } 
    size_t j = 0; 
    for(size_t i=0;i<y_;++i){ 
    v_(i,j)=value; 
    } 
    j = x_; 
    for(size_t i=0;i<y_;++i){ 
    v_(i,j)=value; 
    } 
    } 

我得到一個錯誤,每當我試圖使用重載的()運算符,fillsetBoundary函數內部

((Grid*)this)->Grid::v_不能用作函數

。我曾嘗試在網上尋找類似的錯誤,但不幸的是無法取得很大進展。你有什麼建議,因爲我認爲重載操作符的實現是正確的,據我所知,我沒有命名任何與成員變量同名的函數。

回答

3

v_real*,即指針。指針沒有operator(),所以你不能寫v_(something)

您已爲Grid課程提供了operator()的超載。如果要使用該過載,則需要在Grind類的對象上使用()v_不是您的Grid類的對象。

您可能想調用operator()當前對象(即調用fillsetBoundary的對象)。要做到這一點,你會寫(*this)(arguments)

3

當您編寫v_(i,j)時,您不會調用過載的()運算符。您應該嘗試使用(*this)(i,j)

相關問題