Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: ambiguates old declaration ‘double round(double)’

Tags:

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

Why is my compiler showing an error

like image 201
user1720241 Avatar asked Dec 26 '12 20:12

user1720241


1 Answers

The error is pretty explicit here. The <cmath> header is already introducing a function double round(double) and you can't overload based on return type. Yes, it's defined in the std namespace but you are doing using namespace std; (it's also implementation defined whether it is first defined in the global namespace before it is injected into std). To e completely portable, you'll need to give your function a different name or stick it in another namespace - or, of course, use the round function that <cmath> gives you. But get rid of that using namespace std; too.

like image 73
Joseph Mansfield Avatar answered Oct 04 '22 17:10

Joseph Mansfield