错误:对旧声明“double-round(double)”存在歧义
error: ambiguates old declaration ‘double round(double)’
/usr/include/i386-linux-gnu/bits/mathcalls.h:311:1: error: ambiguates old declaration ‘double round(double)’ g.cpp: In function ‘int round(double)’: g.cpp:14:24: error: new declaration ‘int round(double)’ /usr/include/i386-linux-gnu/bits/mathcalls.h:311:1: error: ambiguates old declaration ‘double round(double)’
#include <iostream>
#include <cmath>
using namespace std;
int round(double number);
int main()
{
double number = 5.9;
round(number);
return 0;
}
int round(double number)
{
return static_cast<int>(floor(number + 0.5));
}
为什么我的编译器显示错误
这里的错误非常明显。<cmath>
标头已经引入了函数double round(double)
,并且不能基于返回类型进行重载。是的,它是在std
命名空间中定义的,但您正在执行using namespace std;
(它也是实现定义的,即在注入std
之前是否首先在全局命名空间中定义它(。要实现完全可移植性,您需要为函数指定一个不同的名称,或者将其粘贴在另一个命名空间中——当然,也可以使用<cmath>
提供的round
函数。但也要去掉using namespace std;
。
相关文章:
- C++模板来检查友元函数的存在
- 既然存在危险,为什么项目要使用-I include开关
- 我们可以访问一个不存在的联盟的成员吗
- C++:对不存在的命名空间使用命名空间指令
- C++quit()函数中可能存在作用域问题
- C++擦除(如果存在)
- g++ 说函数不存在,即使包含正确的标头
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 有了gcc,是否可以链接库,但前提是它存在
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- C++在数学计算中将double转换为int
- C++Builder中的OnClick事件签名存在问题
- 如何正确地将分支添加到已存在的树中
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 如何检查QList中是否存在值
- 根据某个函数是否存在启用模板
- 为什么 g++ 声明某些 valarray o 存在"no matching function for call cbegin(o)"<double>?
- 映射和节点,错误智能感知:不存在从"const std::pair<const int, double>"到"Node"的合适用户定义转换
- 错误:对旧声明“double-round(double)”存在歧义