2013-05-26 87 views
0

下面是一个.cpp文件,我将其包含在另一个.cpp文件中。我收到一个错误,指出emp.grosPay()必须是一个可修改的左值。任何想法修复它?必须是可修改的值错误?

#include <iostream> 
#include <string> 
#include "weeklyEmp.h" 

using namespace std; 

const double WEEKLY_ALLOWANCE = 39.42; 
const double FICA_TAX_RATE = 0.0765; 
weeklyEmp::weeklyEmp() 
{ 
    my_name = ?name?; 
} 
weeklyEmp::weeklyEmp(string initName, 
    double initHours, 
    double initRate, 
    int initExemptions, 
    string initFilingStatus) 
{ 
    my_name; initName; 
    my_hours; initHours; 
    my_rate; initRate; 
    my_exemptions; initExemptions; 
    my_filingStatus; initFilingStatus; 
} 

//--modifiers 

void weeklyEmp::set_hours(double thisWeeksHours) 
    //post: Set the hours worked for a given week 
{ 
    my_hours = thisWeeksHours; 
} 



void weeklyEmp::set_rate(double thisWeeksRate) 
//post: Change the employee's hourly rate of pay 
{ 
my_rate = thisWeeksRate; 
} 

//--accessors 
double weeklyEmp::grosPay() const 
//post: Return gross pay with overtime 

{if(my_hours <=40) return my_hours * my_rate; 
else 
return (40*my_rate) + (my_hours-40) * 1.5 * my_rate; 
} 

double weeklyEmp::incomeTax() const 
//post: Return the federal income tax 
{ 
double result(0.0); 
double taxableIncome(grosPay() - my_exemptions * WEEKLY_ALLOWANCE); 
if(my_filingStatus == "S" || my_filingStatus == "s") 
{ 
if (taxableIncome <= 23.00) 
result = 0.00; 
else if(taxableIncome <= 397.00) 
result = 0.15 * (taxableIncome - 23.00); 
else if(taxableIncome <= 928.00) 
result = 56.10 + 0.28 * (taxableIncome - 397.00); 
else if(taxableIncome <= 2121.00) 
result = 204.78 + 0.33 * (taxableIncome - 928.00); 
else 
result = 598.47 + 0.28 * (taxableIncome - 2121.00); 
} 
if(my_filingStatus == "M" || my_filingStatus == "m") 
{ 
if(taxableIncome <= 65.00) 
result = 0.00; 
else if(taxableIncome <= 689.00) 
result = 0.15 * (taxableIncome - 65.00); 
else if(taxableIncome <= 1573.00) 
result = 93.60 + 0.28 * (taxableIncome - 689.00); 
else if(taxableIncome <= 3858.00) 
result = 341.12 + 0.33 * (taxableIncome - 1573.00); 
else 
result = 1095.17 + 0.28 * (taxableIncome - 3858.00); 
} 
/* round to the nearest penny */ 
/* include compfun.cpp for round function */ 
result =(result, 2); 
return result; 
} 

double weeklyEmp::FICATax() const 
//post: Return the social security tax 
{ 
    return grosPay() * FICA_TAX_RATE; 
} 

string weeklyEmp::name() const 
//post: Return the employee's name 

{ return my_name; 

} 

的代码有错误的部分是下面,标志着* **

int main() 
{ 
    string name; 
    double rate; 
    double hours; 
    int exemptions; 
    string filingStatus; 


    cout <<"Name: "; 
    cin >> name; 
    cout << "Hourly Rate:"; 
    cin >> rate; 
    cout << "Hours Worked:"; 
    cin >> hours; 
    cout << "Exemptions: "; 
    cin >> exemptions; 
    cout<< "S) ingle/M) arried: "; 
    cin >> filingStatus; 
    cout << " " << endl; 

    weeklyEmp emp(name, hours, rate, exemptions, filingStatus); 
    double net = ***emp.grosPay()*** = emp.incomeTax() - emp.FICATax(); 

} 
+0

'grosPay()'返回一个右值,你不能指定给它。这条线的意图是什么? –

+0

什么是my_name =?名称? – chris

+0

我加了“?name?”这是一个错别字 –

回答

0
double net = emp.grosPay() = emp.incomeTax() - emp.FICATax(); 
//      ^^^ 

grosPay不返回的引用,所以我假设你没有意思是在那里使用=标志。可能是一个大拇指。

您是不是要找使用减号来-

double net = emp.grosPay() - emp.incomeTax() - emp.FICATax(); 
//      ^^^ 

错误发生在,因为你不能修改const对象,由const函数返回的对象,或者说”不是个右值t绑定到一个非const的右值引用。

+0

这就是它。谢谢。 –

+0

@ user2395688没问题。:) – 0x499602D2

+0

共识不仅在解决方案上,而且在使用插入符号。 –

0
double net = ***emp.grosPay()*** = emp.incomeTax() - emp.FICATax(); 
           ^^^ 

第二个'='应该是' - '符号;我认为你想要减去,而不是分配。

相关问题