Dec 10, 2010 at 4:33am UTC
This will be an easy one.
How can I access a base class public method from a derived class method. I don't know why I'm having trouble with this. Here is some code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
namespace lap{
class Matrix{
public :
double & operator ()(unsigned int row, unsigned int col);
}
class RowVector : public Matrix{
public :
double & operator ()(unsigned int col) { return (*this )(0,col); }
}
}
I get the following error:
no match for call to '(const lap::RowVector) (int, const unsigned int&)
candidates are: double& lap::RowVector::operator()(unsigned int) const
When I replace it with it works fine:
1 2 3 4 5 6
class RowVector : public Matrix{
public :
double & operator ()(unsigned int col) { return lap::Matrix::operator ()(0,col);
}
Not sure what is going on here. Any help would be great.
Thanks! Nick.
Last edited on Dec 10, 2010 at 4:33am UTC
Dec 10, 2010 at 6:07am UTC
I'm not completely sure why your solution is working really but...
this
is a pointer to RowVector since you are in the RowVector class, and there using the operator() will only look at the RowVector's methods. You will have to use this ->Matrix::operator ()(0, col)
EDIT: You could also use a cast to cast it to your base class then call operator().
Last edited on Dec 10, 2010 at 6:08am UTC
Dec 10, 2010 at 7:18am UTC
Last edited on Dec 10, 2010 at 7:18am UTC
Dec 10, 2010 at 10:34am UTC
Thanks to both of you. All worked out!