I am currently doing some socket programming using C/C++. To be able to use a somewhat cleaner interface, and a more OO structure, I decided to write a few simple wrapper classes around parts of the C socket API, but while doing so I stumbled upon a problem:
Given the following code:
// Global method int foo(int x) { return x; } // Class that calls the global method class FooBar { public: void foo() { return; }; void baz() { foo(1); } };
g++ gives the following error message:
test.cpp: In member function ‘void FooBar::baz()’: test.cpp:10: error: no matching function for call to ‘FooBar::foo(int)’ test.cpp:9: note: candidates are: void FooBar::foo()
Renaming the class method solves the problem.
Why is it that there is some kind of naming conflict even though the method signatures are different? What is the best way to fix this?
Thanks /Erik
The problem is that it first looks in the scope of your class, and finds a foo function. The lookup will stop then, and the compiler tries to match arguments. Since it only has the one foo function in that scope in your class, calling the function fails.
You need to explicitly state that you want to call the free function:
Another solution is to declare the function within baz:
The scope that the foo function is assumed to be defined in is the surrounding namespace of your class.