为什么在使用“using namespace std;”时在这段代码中出现错误?和“位/stdc++.h”?

Pra*_*eja 3 c++ namespaces function ambiguous name-lookup

实际上这段代码在“DEV C++”中运行良好,但是当我将它放入我的“Hacker-Rank”面板时,它给出了这个错误“对函数的引用不明确”,尽管所有在线编译器都给出了错误......

我不认为这里的函数重载是中断的地方,因为这个错误主要来自函数重载。

#include <bits/stdc++.h>
#include <cstdio>
#include<iostream>

using namespace std;


int function(int n);

int main()
{
    int n;
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');    

    if(n<=0){
        return(0);
    }
    else{
        function(n);
    }

}
int function(int n)
{
    if (n<=9)
    {
        cout<<"experiment";
    }
    
    else{
        cout<<"Greater than 9";
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

clang 的错误是

<source>:20:9: error: reference to 'function' is ambiguous
        function(n);
        ^
<source>:8:5: note: candidate found by name lookup is 'function'
int function(int n);
    ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/std_function.h:111:11: note: candidate found by name lookup is 'std::function'
    class function;
          ^
// ... and more ....
Run Code Online (Sandbox Code Playgroud)

Vla*_*cow 5

对于初学者这个 else 代码块

else{
    function(n);
}
Run Code Online (Sandbox Code Playgroud)

什么都不返回。

虽然它是允许的,但会使程序的读者感到困惑,因为他们期望如果在 if 子语句中存在显式 return 语句,那么在 else 子语句中应该有一个类似的 return 语句。

由于 using 指令function,在全局命名空间中声明的名称似乎与标准名称冲突std::function

using namespace std;
Run Code Online (Sandbox Code Playgroud)

else{
    return ::function(n);
}
Run Code Online (Sandbox Code Playgroud)