错误:模糊旧声明'double round(double)'

时间:2012-12-26 20:05:59

标签: c++

/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));
}

为什么我的编译器显示错误

1 个答案:

答案 0 :(得分:13)

这里的错误很明显。 <cmath>标头已经引入了函数double round(double),您不能基于返回类型重载。是的,它在std命名空间中定义,但您正在执行using namespace std;(它也是实现定义它是否在注入std之前首先在全局命名空间中定义)。为了完全可移植,您需要为您的函数指定一个不同的名称或将其粘贴到另一个名称空间中 - 或者,当然,使用round给您的<cmath>函数。但也要摆脱using namespace std;

相关问题