"未在此范围例外中声明"在调用类函数时

时间:2017-11-22 09:38:09

标签: c++ function

#include <iostream>
#include <string>
using namespace std;

struct Employee{
int eno;
string ename;
string job;
float salary;
float bonus;
};

float emp_salary (string emp_job);
float emp_bouns (string emp_job, float emp_salary);

int main(){
Employee emp[5];

cout << "Please enter the employees' details: " << endl;
for(int i = 0; i < 5; i++){
    cout << "Employee no." << (i+1) << " : " << endl
    << "Employee number : ";
    cin >> emp[i].eno;

    cout << "Name : ";
    cin >> emp[i].ename;

    cout << "Job : ";
    cin >> emp[i].job;
}

for(int i = 0; i < 5; i++){
    emp[i].salary = emp_salary(emp[i].job);
    emp[i].bonus = emp_bonus(emp[i].job, emp[i].salary);
    }

cout << endl << "------------------------------------" << endl;
cout << "Employee's information:";
for(int i = 0; i < 5; i++){
    cout << "Employee no." << (i+1) << " : "
    << "Employee number : " << emp[i].eno << endl
    << "Name : " << emp[i].ename << endl
    << "Job : " << emp[i].job << endl
    << "Salary : " << emp[i].salary << endl
    << "Bonus : " << emp[i].bonus << endl;
}
}
float emp_salary (string job){
float salary;
if(job == "Manager"){
    salary = 5000;
}
else if(job == "Engineer"){
    salary = 3000;
}
else if(job == "Clerk"){
    salary = 2000;
}
else{
    salary = 1000;
}
return salary;
}

float emp_bouns (string emp_job, float emp_salary){
float bonus;
if(emp_job == "Manager"){
    bonus = emp_salary * 1.00;
}
else if(emp_job == "Engineer"){
    bonus = emp_salary * 0.80;
}
else if(emp_job == "Clerk"){
    bonus = emp_salary * 0.60;
}
else{
    bonus = emp_salary * 0.40;
}
return bonus;
}

编写本程序是为了从用户那里获得有关员工详细信息的输入,此处的函数emp_bonus用于根据工资乘以基于员工工作的百分比计算他们收到的奖金。

当我调用函数emp_bonus()时显示错误,说emp_bonus未在此范围内声明,这是什么意思?我应该在声明,定义或调用

中使用正确的参数

2 个答案:

答案 0 :(得分:1)

在你的代码中,你已经命名了你的函数emp_boUns但是当你打电话给那个方法时,你并没有犯这个错字。这就是为什么你收到错误说明emp_bonus方法没有被声明的原因。

修改:我建议您查看Rubbber ducky debugging。这是解决这个(以及许多其他)问题的有效对策。

答案 1 :(得分:1)

您声明emp_b oun 并呼叫emp_b onu s