這裏的,我有一個頭文件中創建一個類的摘錄:*這個 - >不能被用作函數
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;
}
}
我得到一個錯誤,每當我試圖使用重載的()
運算符,fill
和setBoundary
函數內部
((Grid*)this)->Grid::v_
不能用作函數
。我曾嘗試在網上尋找類似的錯誤,但不幸的是無法取得很大進展。你有什麼建議,因爲我認爲重載操作符的實現是正確的,據我所知,我沒有命名任何與成員變量同名的函數。