如何解决终端的这个错误?

时间:2016-08-03 09:02:26

标签: c++

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

错误如下:

 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:在函数'int round(double)'中:            roundoff.cpp:22:24:错误:新声明'int round(double)'            int round(双号)                         ^       在/usr/include/features.h:374:0中包含的文件中,                    来自/usr/include/x86_64-linux-gnu/c++/4.8/bits /os_defines.h:39,                  来自/usr/include/x86_64-linux-gnu/c++/4.8/bits /c++config.h:426,                    来自/usr/include/c++/4.8/iostream:38,                  来自roundoff.cpp:1:    /usr/include/x86_64-linux-gnu/bits/mathcalls.h:309:1:错误:ambiguates旧声明'double round(double)'     MATHCALLX(round ,,( Mdouble __x),(__ const ));    ^

1 个答案:

答案 0 :(得分:1)

您必须更改函数round的名称,因为cmath已经定义了一个名为round的方法,该方法具有相同的签名,导致歧义。将名称更改为my_round(double number)而不是round,它会起作用。