Reputation: 71
Here's an excerpt of a class which I have created inside a header file :
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
};
and here's an excerpt from the code I have written for the previously declared functions in a separate .cpp file. Note that I have only included parts which were relevant to my error.
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;
}
}
I am getting an error
((Grid*)this)->Grid::v_
cannot be used as a function
whenever I am trying to use the overloaded ()
operator, inside the fill
and setBoundary
function. I have tried looking for similar errors online, but unfortunately couldn't make much progress. Do you have any suggestions, because I think the implementation of the overloaded operator is correct and as far as I know I haven't named any function with the same name as a member variable.
Upvotes: 0
Views: 114
Reputation: 11058
When you write v_(i,j)
, you are not calling the overloaded ()
operator. You should try (*this)(i,j)
instead.
Upvotes: 3
Reputation: 370172
v_
is a real*
, i.e. a pointer. Pointers don't have an operator()
, so you can't write v_(something)
.
You've provided an overload of operator()
for your Grid
class. If you want to use that overload, you need to use ()
on an object of your Grind
class. v_
is not an object of your Grid
class.
You probably want to invoke operator()
on the current object (i.e. the object that fill
or setBoundary
are being called on). To do that, you'd write (*this)(arguments)
.
Upvotes: 3