我目前正在使用 C/C++ 进行一些套接字编程。为了能够使用更简洁的接口和更面向对象的结构,我决定围绕 C 套接字 API 的部分编写一些简单的包装类,但在这样做时我偶然发现了一个问题:
给定以下代码:
// 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++ 给出以下错误消息:
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()
重命名类方法解决了这个问题。
为什么即使方法签名不同,也会存在某种命名冲突?解决此问题的最佳方法是什么?
谢谢/埃里克