c++ - error: cannot convert `double ()(double) throw ()' to `double' in assignment -
can't figure out why have these error messages. brand new c++.
payroll.cpp: in function `void fgross()': payroll.cpp:57: error: assignment of function `double round(double)' payroll.cpp:57: error: cannot convert `double' `double ()(double) throw ()' in assignment payroll.cpp:59: error: cannot convert `double ()(double) throw ()' `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' `double ()(double) throw ()' in assignment payroll.cpp:67: error: cannot convert `double ()(double) throw ()' `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' `double ()(double) throw ()' in assignment payroll.cpp:75: error: cannot convert `double ()(double) throw ()' `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' `double ()(double) throw ()' in assignment payroll.cpp:84: error: cannot convert `double ()(double) throw ()' `double' in assignment payroll.cpp: in function `void fround()': payroll.cpp:110: error: invalid operands of types `double ()(double) throw ()' , `double' binary `operator+' this code:
#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 payroll program!!!\n\n\n"; cout <<"how many hours did work week?\t"; cin >> hours; cout <<"how many children 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 using pp!!" <<"\n\nendeavor have 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; }
there standard library function named round, declared in <cmath>. code expects global variable named round, appear have forgotten declare, of uses of round in fgross, fsoc, etc resolve function. can't assign function name, , can't assign pointer-to-function (which when use function name on right-hand side of assignment) floating-point variable. hence confusing errors.
in order make program work, need change name of variable round; if add missing declaration, error,
test.cc:10:12: error: ‘double round’ redeclared different kind of symbol /usr/include/x86_64-linux-gnu/bits/mathcalls.h:326:1: note: previous declaration ‘double round(double)’ ... @ least give more of clue problem is.
there lot of other problems code, of point out important three:
- all of
f-functions should take arguments , return value; of global variables should removed. (i suspect near-literal translation of cobol, that's next refactor plan do, must point out anyway.) - computations on money must done using fixed-point arithmetic avoid rounding , underflow problems; simplest thing, work enough kind of program, scale integer number of cents , use
int64_tvariables; in computations involving interest compounded on many years, or similar, might want scale 100ths or 1000ths of cent instead. using namespace stdbad practice , cause more problems of type tripped over. writeusing std::foo;each foo need.
Comments
Post a Comment