Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reason of "no matching function".

30 views
Skip to first unread message

m.laba...@gmail.com

unread,
Aug 8, 2018, 8:51:11 AM8/8/18
to
Hello,

Following program can not be compiled by g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609:

--[beg]--[main.cpp]--

#include <iostream>

class A { public:
void bar(int value) {
std::cout << "bar:" << value << std::endl;
}
void foo(int value) {
std::cout << "foo:" << value << std::endl;
}
};

class B : public A { public:
void foo() {
std::cout << "void" << std::endl;
}
};

int main(void)
{
B b;

int i = 1234;

b.foo();

b.bar(i);

b.foo(i); /* Error is here -> to avoid error 'b.A::foo(i)' has to be called */

return 0;
}

--[end]--[main.cpp]--

# g++ main.cpp
main.cpp: In function ‘int main()’:
main.cpp:29:10: error: no matching function for call to ‘B::foo(int&)’
b.foo(i); /* Error is here -> to avoid error b.A::foo(i) has to be called */
^
main.cpp:14:8: note: candidate: void B::foo()
void foo() {
^
main.cpp:14:8: note: candidate expects 0 arguments, 1 provided


I don't see any conflicts.
'bar' function implementation is found correctly in A.
Why compiler is not able to find automatically 'foo(int)' implementation in A?

Regards

--
Maciej Labanowicz

Paavo Helde

unread,
Aug 8, 2018, 9:24:02 AM8/8/18
to
foo in b is hiding inherited foo from A. This is a safety feature to
make the code more robust against later changes.

To unhide A::foo, add the following line into B:

using A::foo;



chris...@engineer.com

unread,
Aug 9, 2018, 8:32:26 PM8/9/18
to
0 new messages