Dhavalkumar Prajapati Dhavalkumar Prajapati - 3 months ago 25
C++ Question

How to solve this error?of terminal

#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;
int round(double number);
int main()
{
double doublevalue;
char ans;
do
{
cout << "Enter the double value:";
cin >> doublevalue;
cout << "Rounded that number is"<<round(doublevalue)<<endl;
cout << "Again?(y/n)";
cin >> ans;
}while(ans=='y' ||ans=='Y');
cout << "End of testing.\n";
return 0;
}
int round(double number)
{
return static_cast<int>(floor(number+0.5));
}


error is as below:

roundoff.cpp:6:24: error: new declaration ‘int round(double)’
int round(double number);
^
In file included from /usr/include/features.h:374:0,
from /usr/include/x86_64-linux-gnu/c++/4.8/bits/os_defines.h:39,
from /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h:426,
from /usr/include/c++/4.8/iostream:38,
from roundoff.cpp:1:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:309:1: error: ambiguates old declaration ‘double round(double)’
__MATHCALLX (round,, (_Mdouble_ __x), (__const__));


^
roundoff.cpp: In function ‘int round(double)’:
roundoff.cpp:22:24: error: new declaration ‘int round(double)’
int round(double number)
^
In file included from /usr/include/features.h:374:0,
from /usr/include/x86_64-linux-gnu/c++/4.8/bits /os_defines.h:39,
from /usr/include/x86_64-linux-gnu/c++/4.8/bits /c++config.h:426,
from /usr/include/c++/4.8/iostream:38,
from roundoff.cpp:1:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:309:1: error: ambiguates old declaration ‘double round(double)’
MATHCALLX (round,, (Mdouble __x), (__const));
^

Answer

You must change the name of your function round because cmath already defines a method called round with same signature causing the ambiguity. Change the name to my_round(double number) instead of round and it will work.