I'm trying to call overloaded virtual function which fails with "no matching function" error.
Here's mwe.
#include <iostream>
#include <string>
#include <sstream>
class A {
public:
virtual int foo(int a, int b) {
// interface
return 1;
}
protected:
virtual int foo(std::string a, std::string b, std::string c) {
// actual work
std::cout << "A::foo(str...) " << a << " " << b << " " << c << std::endl;
return 0;
}
};
class B : public A {
public:
virtual int foo(int a, int b) override {
// some work. e.g. parse arguments
foo("a", "b", "c"); // no matching function call
return 0;
}
};
int main(void) {
B b;
A* bb = static_cast<A*>(&b);
bb->foo(1, 2);
}
I expect interface foo(int, int) to be called as normal virtual function. If object is of derived class, it should call foo(int, int) override which, in it's turn calls foo (string, string, string) as normal virtual function. Since it's not overriden in B, A::foo(string, string, string) should be called.
I'm getting "no matching function call" instead.
On the other hand, if I override foo(string, string, string) in B, virtual functions & inheritance work as I would expect. Override works.
mwe:
#include <iostream>
#include <string>
#include <sstream>
class A {
public:
virtual int foo(int a, int b) {
// interface
return 1;
}
protected:
virtual int foo(std::string a, std::string b, std::string c) {
// actual work
std::cout << "A::foo(str...) " << a << " " << b << " " << c << std::endl;
return 0;
}
};
class B : public A {
public:
virtual int foo(int a, int b) override {
// some work. e.g. parse arguments
foo("a", "b", "c");
return 0;
}
protected:
virtual int foo(std::string a, std::string b, std::string c) {
// actual work
std::cout << "B::foo(str...) " << a << " " << b << " " << c << std::endl;
return 0;
}
};
int main(void) {
B b;
A* bb = static_cast<A*>(&b);
bb->foo(1, 2);
}
so in first example call to foo(string, string, string) fails to find A::foo(string, string, string), but in second example override does it with no issues. So the function is in virtual function table.
So what happens in first case?