以下代码为什么是非法的?
使用clang和
我知道如何解决这个问题,但我想知道为什么编译器认为我是在定义函数 (
#include <iostream>
using namespace std;
namespace what {
void print(int count) {
cout << count << endl;
}
}
void what::print(const string& str) {
cout << str << endl;
}
int main() {
what::print(1);
what::print("aa");
return 0;
}
使用clang和
-std=c++14
编译时出现的错误是:error: out-of-line definition of 'print' does not match any declaration in namespace 'what'
我知道如何解决这个问题,但我想知道为什么编译器认为我是在定义函数 (
print
) 而不是重载它。
what::
不就是试图做到这一点吗?我认为重点应该更多地放在在相同的作用域内上。试图从不同的命名空间重载函数肯定是不同的作用域。 - Mr. Llama