Question

I have a base class that comprises an abstract method func(int, float, unsigned) and an overload to this method func(int), and a Derived class that implements the abstract method .

class Base
{
public:
    virtual void func(int x, float y, unsigned z) = 0;

    void func(int x)
    {
        cout << "func with x only" << endl;
    }
};

class Derived : public Base
{
public:
    void func(int x, float y, unsigned z)
    {
        cout << "func override" << endl;
    }

};

In my code, I have an instance of the derived class that calls the overloaded method of the base func(int).

int main()
{
    Derived d;
    d.func(10);     // <<--------- 'COMPILATION ERROR'
    return 0;
}

At compiling this piece of code, I get the following compilation error:

error: no matching function for call to 'Derived::func(int&)'
note: candidates are: virtual void Derived::func(int, float, unsigned int)

What 's the reason for this error / why this code does not work ?

Was it helpful?

Solution

You need to bring the base class function into the derived class' namespace.

Do this by writing

using Base::func;

somewhere in the declaration of your child class.

Note that you are overloading func, not overriding it.

OTHER TIPS

In c++11 :

int main()
{
    Base && b = Derived();
    b.func(10);    
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top