I've got a base class with a few pure virtuals, and I want it to provide default implementations of several methods which can be overridden if needed. I've boiled my problem down to the following:
class A {};
class B {};
class Base {
public:
virtual void foo (const A&) {};
virtual void foo (const B&) = 0;
};
class Derived : public Base {
public:
virtual void foo(const B&) {};
};
int main(int argv, char** argc) {
Derived d;
d.foo(A());
return 0;
}
For reasons I don't understand at all, this fails on g++ 4.8.2:
g++ scratch.cpp -o scratch
scratch.cpp: In function ‘int main(int, char**)’:
scratch.cpp:17:12: error: no matching function for call to ‘Derived::foo(A)’
d.foo(A());
^
scratch.cpp:17:12: note: candidate is:
scratch.cpp:12:16: note: virtual void Derived::foo(const B&)
virtual void foo(const B&) {};
^
scratch.cpp:12:16: note: no known conversion for argument 1 from ‘A’ to ‘const B&’
If I define foo(const B&)
in Base
and leave Derived
empty, the problem goes away. I've read the docs and some of the questions here, but nothing has suggested why this would be a problem. In my real code, Base::foo(const A&)
will build a special object b
of type B
from the A
object, and then call foo(b)
.
Add this line to your Derived
class definition:
using Base::foo;
This will solve the problem that your foo(B) definition in Derived hides both foo(A) and foo(B) functions in Base.