错误:无法在赋值时将`double()(double)throw()'转换为`double'

时间:2017-07-28 16:12:52

标签: c++

无法弄清楚为什么我会收到这些错误消息。我是C ++的新手。

payroll.cpp: In function `void fGross()':
payroll.cpp:57: error: assignment of function `double round(double)'
payroll.cpp:57: error: cannot convert `double' to `double ()(double)
throw ()' in assignment
payroll.cpp:59: error: cannot convert `double ()(double) throw ()'
to `double' in assignment
payroll.cpp: In function `void fSoc()':
payroll.cpp:65: error: assignment of function `double round(double)'
payroll.cpp:65: error: cannot convert `double' to `double ()(double)
throw ()' in assignment
payroll.cpp:67: error: cannot convert `double ()(double) throw ()'
to `double' in assignment
payroll.cpp: In function `void fFed()':
payroll.cpp:73: error: assignment of function `double round(double)'
payroll.cpp:73: error: cannot convert `double' to `double ()(double)
throw ()' in assignment
payroll.cpp:75: error: cannot convert `double ()(double) throw ()'
to `double' in assignment
payroll.cpp: In function `void fSt()':
payroll.cpp:82: error: assignment of function `double round(double)'
payroll.cpp:82: error: cannot convert `double' to `double ()(double)
throw ()' in assignment
payroll.cpp:84: error: cannot convert `double ()(double) throw ()'
to `double' in assignment
payroll.cpp: In function `void fround()':
payroll.cpp:110: error: invalid operands of types `double ()(double)
throw ()' and `double' to binary `operator+'

这是我的代码:

#include <iostream>
#include <climits>
#include <cmath>

using namespace std;

    double hours, gross, socSec, fedTax, stTax, ins, net;
    double const rate = 16.78;
    double unin = 10.00;
    char d = '$';
    void fGross();
    void fSoc();
    void fFed();
    void fSt();
    void fUnin();
    void fIns();
    void fNet();
    void fround();


int main (void)
{
        cout <<"\n\n\t\tWelcome to the Payroll Program!!!\n\n\n"; 

        cout <<"How many hours did you work this week?\t";
        cin >> hours;
        cout <<"How many children do you have?";
        cin >> ins;

        fGross();
        fSoc();
        fFed();
        fSt();
        fUnin();
        fNet (); 

        cout <<"Payroll Stub:\n\n\t"
             <<"Hours:\t" << hours
             <<"\n\tRate:\t" << rate <<"16.78 $/hr\n\t"
             <<"Gross:\t" << d << gross
             <<"\n\n\tSocSec:\t" << d << socSec
             <<"\n\tFedTax:\t" << d << fedTax
             <<"\n\tStTax:\t" << d << stTax
             <<"\n\tUnion:\t" << d << unin;

        fIns();

        cout <<"\n\tNet:\t" << net <<"\n\nThank you for using the PP!!"
             <<"\n\nEndeavor to have a StarTrek-esque day!";

        return 0;
}    


void fGross()
{
    round = rate * hours;
    fround();
    gross = round;
    return;
}

void fSoc()
{
    round = gross * .06;
    fround();
    socSec = round;
    return;
}

void fFed()
{
    round = gross * .14;
    fround();
    fedTax = round;
    return;

}

void fSt()
{
    round = gross * .05;
    fround();
    stTax = round;
    return;
}

void fIns()
{
    if (ins >= 3)
    {
        ins = 35.00;
        cout << "\n\tIns:\t" << d << ins;
    }
    else
    {
        ins = 0.00;
    }
    return;
}

void fNet()
{
    net = gross - socSec - fedTax - stTax - unin - ins;
    return;
}

void fround()
{
    round = floor((round + .05) * 100);
    return;  
}

1 个答案:

答案 0 :(得分:1)

<cmath>中声明了一个名为round的标准库函数。您的代码需要一个名为round的全局变量,您似乎忘记了声明,因此roundfGross的所有用法,{{1等等解析函数。您不能将任何内容分配给函数名,并且您不能将指向函数的指针(在赋值的右侧使用函数名时获得的函数)分配给浮点变量。因此所有令人困惑的错误。

为了使程序有效,您需要更改变量fSoc的名称;如果您只是添加缺少的声明,您将收到另一个错误,

round

...这至少可以让你更清楚问题是什么。

此代码存在其他问题很多,其中我只会指出最重要的三个:

  • 所有test.cc:10:12: error: ‘double round’ redeclared as different kind of symbol /usr/include/x86_64-linux-gnu/bits/mathcalls.h:326:1: note: previous declaration ‘double round(double)’ - 函数都应该接受参数并返回一个值;应删除所有全局变量。 (我怀疑这是一些近乎字面的COBOL翻译,所以希望这是你打算做的下一个重构,但无论如何我必须指出它。)
  • 使用fixed-point算法完成对的计算以避免舍入和下溢问题;对于这种程序来说效果很好的最简单的事情是将所有内容扩展为整数分,然后使用f个变量;在涉及多年或类似的利息的计算中,你可能想要扩大到百分之一或千分之一的分数。
  • int64_t是不好的做法,会导致你绊倒的类型出现更多问题。为你实际需要的每个foo写using namespace std