我不明白为什么会出现“对重载函数的模糊调用”错误。 在“main”之前,我声明使用命名空间“second”。 我的预期输出是:
this is the first foo
this is the second foo
this is the second foo
#include <iostream>
using namespace std;
namespace first {
void foo() {
cout << "this is the first foo" << endl;
}
}
namespace second {
void foo() {
cout << "this is the second foo" << endl;
}
}
void foo() {
cout << "this is just foo" << endl;
}
using namespace second;
void main() {
first::foo();
second::foo();
foo();
}
最佳答案
Before "main", I declared to use namespace "second".
当你这样做时,second::foo
被引入全局命名空间,然后对于 foo();
两个 second::foo
和 ::foo
是有效的候选对象。
您可以明确指定要调用全局 foo
,即
::foo();
或者使用using-declaration在 main()
中而不是 using-directive ,例如
int main() {
using second::foo;
first::foo();
second::foo();
foo();
}
关于c++ - 使用命名空间和重载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465238/