3

I am having trouble making a derived class access a function that is defined in the base class. The base class header file, called Particle.h, is:

class Particle {
    protected:
        vector<double> x_,y_,z_;

        // lots of other protected members that are not relevant to the question

    public:

        // constructors and other functions not relevant to question are omitted
        virtual double getX(const unsigned int index, const double theta, const double phi);
        virtual vector<double> getX(const double theta, double Real phi);
        vector<double> getX(const unsigned int surfindex);
}

The definition of this function is in a file called Particle.cc:

#include "Particle.h"

vector<double> Particle::getX(const unsigned int surfindex)
{
    vector<double> xp;
    xp.clear();
    xp.resize(3,0.0);
    xp[0] = x_.at(surfindex);
    xp[1] = y_.at(surfindex);
    xp[2] = z_.at(surfindex);

    return xp;
}

The derived class header file, called Star.h, is:

#include "Particle.h"

using namespace std;

class Star : public Particle {

    public:

       // constructors and other functions not relevant to question are omitted here

       virtual void computeRoundness(const unsigned int method);
       double getX(const unsigned int index, const double theta, const double phi);
       vector<double> getX(const double theta, double Real phi);
}

The definition of the computeRoundness function is in a file called Star.cc:

#include "Star.h"

// Lots of other function definitions not relevant to question are omitted here

void Star::computeRoundness(const unsigned int method)
{
    vector<double> X;
    unsigned int count = 0;
    while (count < ntheta) {
       X = getX(count);      // Should call base class function, right?

       // do some more things with X that are not relevant to this question
    }
}

But I receive this compile-time error:

Star.cc: In member function ‘virtual void Star::computeRoundness(unsigned int)’:
Star.cc:1340: error: no matching function for call to ‘Star::getX(unsigned int&)’
Star.h:687: note: candidates are: virtual double Star::getX(unsigned int, double, double)
Star.h:696: note:                 virtual std::vector<double, std::allocator<double> > Star::getX(double, double)

I have successfully called base class functions from derived classes in other C++ projects in the past, so I must be overlooking something simple here, but I just can't find it. I thought that base class functions should be inherited by derived classes unless they are declared virtual and then overridden in the derived class (but that is not the case here), even if the derived class overloads the function name as I have done here a couple of times. Is that not true? And if not, is there anything elegant I can do to fix the problem rather than just redefining the same functions in my derived class?

Thanks very much for any help.

Jeff Bullard
  • 345
  • 1
  • 4
  • 14
  • possible duplicate of [Function with same name but different signature in derived class](http://stackoverflow.com/questions/411103/function-with-same-name-but-different-signature-in-derived-class) – Robᵩ Sep 26 '13 at 20:59
  • 1
    I like [my previous answer](http://stackoverflow.com/a/6035884/8747) to a similar question. – Robᵩ Sep 26 '13 at 21:01

3 Answers3

6

You have to either add using getX; in the derived class, or use particle::getX in the function.

The standard says that you don't automatically use the base class' function if the derived has a function of the same name - even if the derived's function doesn't fit. It's to prevent bugs.

You have to either tell the derived class it will be using the base class' function (by having using getX; direvtive) OR explicitly call the base class' function (by calling particle::getX(...))

rabensky
  • 2,864
  • 13
  • 18
0

The declaration of the function getX in the base class is:

virtual vector<double> getX(const double theta, double Real phi);

but the definition is:

vector<double> Particle::getX(const unsigned int surfindex)
{
    //Stuff
}

The signatures doesn't match! change the declaration and the code will work

BTW The copy of one vector is very expensive, consider change your design using references

hidrargyro
  • 257
  • 1
  • 7
0

The answer is this: If the base class has 'n' number of overloaded versions of a function and in your derived class you redefine/override(change the body) or overload(change the signature) of that function, then only that version in the derived class is available to you.

Sample:

class Base
{
    public:
    int abc(){}
    float abc(){}
};

class Der:public Base
{
    public:
    SomeClassObj abc(){} //int and float returning versions of abc are now hidden for Der object
};

Same is the case for Overriding/Redefinition.

NotAgain
  • 1,927
  • 3
  • 26
  • 42
  • Does that mean that I need to recopy every unchanged base class function in the derived class if I have overloaded/overridden functions with the same name? That seems to potentially force the duplication of a lot of code. Is that an indication of poor design on my part? – Jeff Bullard Sep 27 '13 at 14:31