错误:无法在赋值中将"双 ()(双)抛出 ()"转换为"双倍"
error: cannot convert `double ()(double) throw ()' to `double' in assignment
无法弄清楚为什么我有这些错误消息。我是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
的全局变量,你似乎忘记声明了它,所以round
在fGross
、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)’
。这至少会让你更多地了解问题所在。
这段代码还有很多其他问题,其中我只指出最重要的三个:
- 所有
f
函数都应该接受参数并返回一个值;应该删除所有全局变量。(我怀疑这是一些 COBOL 的近乎字面翻译,所以希望这是您计划进行的下一个重构,但无论如何我都必须指出它。 - 货币计算必须使用定点算法来完成,以避免四舍五入和下溢问题;最简单的事情,对于这种程序来说已经足够好了,是将所有东西缩放到整数美分,然后使用
int64_t
变量;在涉及多年复利或类似利息的计算中,你可能希望扩展到100分或1000分之一美分。 using namespace std
是不好的做法,会导致更多您绊倒的类型问题。 为您实际需要的每个 foo 写using std::foo;
。
相关文章:
- 从'size_t'转换为"常量双倍",可能会丢失数据
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- 如何将包含指数的 QString 转换为C++中的双倍?
- 将双倍转换为 LPARAM 的麻烦
- 如何将标准::string_view转换为双倍?
- 错误:无法在赋值中将"双 ()(双)抛出 ()"转换为"双倍"
- 将双倍转换为字符数组C++
- 从双倍到时间的转换
- 如何将标准::时间::d转换为双倍(秒)?
- 缩小从字符到双倍的转换
- 使用霍华德·欣南特的日期库将双倍转换为zoned_time
- 将词法强制转换双倍提升为字符串,给出无效结果
- 它不是编译.我正在调用一个通过引用调用的函数,但有一个错误,无法将双*转换为双倍
- 如何避免警告C4244:从'ULONGLONG'转换为"双倍",可能丢失数据
- 如何在C++中从尾数和指数转换为双倍
- C++ 将双倍转换为字符并替换为标准::数组
- 数据类型在 CPP 中将 int 转换为双倍
- 使用 QList 将 Qstring 转换为双倍
- 如何习惯性地将“字符*”转换为“双倍*”
- 精确地将双倍转换为mpf_class