Why this code snippet does not compile if there is a member method named Derived::foo(int)
,
#include <string>
class Base
{
public:
int foo(int a, double b){return 0;}
int foo(std::string a, double b){return 0;}
};
class Derived:public Base
{
public:
int foo(int a){return 0;}
int do_sth()
{
foo(2, 3.5);
return 0;
}
};
int main()
{
}
,whereas the code snippet works below well when commenting out the said function.
#include <string>
class Base
{
public:
int foo(int a, double b){return 0;}
int foo(std::string a, double b){return 0;}
};
class Derived:public Base
{
public:
//int foo(int a){return 0;}
int do_sth()
{
foo(2, 3.5);
return 0;
}
};
int main()
{
}
Here is what the compiler complains about the former code snippet:
<source>: In member function 'int Derived::do_sth()':
<source>:17:12: error: no matching function for call to 'Derived::foo(int, double)'
17 | foo(2, 3.5);
| ~~~^~~~~~~~
<source>:13:9: note: candidate: 'int Derived::foo(int)'
13 | int foo(int a){return 0;}
| ^~~
<source>:13:9: note: candidate expects 1 argument, 2 provided
I think foo(2, 3.5);
does exactly match the Base::foo(int, double)
.It's amazing that the compiler complains about the former code snippet.
But since the compiler is almost always right, so I must miss something. Could somebody shed some light on this matter?