【发布时间】:2021-03-15 06:43:06
【问题描述】:
实际上,这段代码在“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;
}
<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 ....
【问题讨论】:
-
请仔细检查一下,即使对于hackerrank 参与,反模式实际上对您有好处。 stackoverflow.com/questions/31816095/…
-
不要
using namespace std; -
如果你已经包含了
<bits/stdc++.h>那为什么还要包含其他标题? -
请不要解释错误信息。将其逐字包含在问题中。该消息包含您不应忽略的有用信息
-
@Yunnosch 我还添加了错误消息,不确定这如何干扰“尽管所有在线编译器都给出错误......”我想这是一个错字(“是 not 给出错误" ?!?)
标签: c++ function namespaces ambiguous name-lookup