user1720241
user1720241

Reputation: 61

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

Why is my compiler showing an error

Upvotes: 5

Views: 18419

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110698

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.

Upvotes: 12

Related Questions