错误:无法在赋值中将"双 ()(双)抛出 ()"转换为"双倍"

error: cannot convert `double ()(double) throw ()' to `double' in assignment

本文关键字:转换 双倍 抛出 赋值 错误      更新时间:2023-10-16

无法弄清楚为什么我有这些错误消息。我是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 <<"nnttWelcome to the Payroll Program!!!nnn"; 
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:nnt"
<<"Hours:t" << hours
<<"ntRate:t" << rate <<"16.78 $/hrnt"
<<"Gross:t" << d << gross
<<"nntSocSec:t" << d << socSec
<<"ntFedTax:t" << d << fedTax
<<"ntStTax:t" << d << stTax
<<"ntUnion:t" << d << unin;
fIns();
cout <<"ntNet:t" << net <<"nnThank you for using the PP!!"
<<"nnEndeavor 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 << "ntIns: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;  
}

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

为了使程序工作,你需要更改变量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)’

。这至少会让你更多地了解问题所在。

这段代码还有很多其他问题,其中我只指出最重要的三个:

  • 所有f函数都应该接受参数并返回一个值;应该删除所有全局变量。(我怀疑这是一些 COBOL 的近乎字面翻译,所以希望这是您计划进行的下一个重构,但无论如何我都必须指出它。
  • 货币计算必须使用定点算法来完成,以避免四舍五入和下溢问题;最简单的事情,对于这种程序来说已经足够好了,是将所有东西缩放到整数美分,然后使用int64_t变量;在涉及多年复利或类似利息的计算中,你可能希望扩展到100分或1000分之一美分。
  • using namespace std是不好的做法,会导致更多您绊倒的类型问题。 为您实际需要的每个 foo 写using std::foo;