I have a head cold so it may be that I am simply too congested to understand what is going on here but I can't figure out how the following doesn't compile?
#include <string>
#include <iostream>
class Base {
public:
virtual void foo(const std::string & data) {
foo(data.data(), data.size());
}
virtual void foo(const void * bytes, int size) = 0;
};
class Derived : public Base{
public:
virtual void foo(const void * bytes, int size) {
std::cout << "Num Bytes: " << size << std::endl;
}
};
int main() {
Derived x;
std::string blah = "Hello";
x.foo(blah);
return 0;
}
The error I get is:
./foo.cpp: In function ‘int main()’:
./foo.cpp:25:15: error: no matching function for call to ‘Derived::foo(std::__cxx11::string&)’
x.foo(blah);
^
./foo.cpp:17:18: note: candidate: virtual void Derived::foo(const void*, int)
virtual void foo(const void * bytes, int size) {
^
./foo.cpp:17:18: note: candidate expects 2 arguments, 1 provided
If I rename the first instance of foo
to fooString
then everything works so it seems the second instance of foo
is somehow hiding the first. However, given that the second instance has two arguments instead of one I can't figure out how it would be ambiguous.
UPDATE: It also works fine without the inheritance (if it is all one class) so I suspect I am misunderstanding some rule of inheritance.