我正在尝试重新创建一个收银机程序。 我有一个员工对象,它有一个名字和一个折扣 %
staffMember me ("tom", 20);
我想将折扣应用于收银机的总额。如果我像这样使用 me.getDiscountPercent 将折扣作为整数参数传递,我的方法就有效
cashy.applyStaffDiscount(me.getDiscountPercent());
void cashRegister::applyStaffDiscount(int discount){
total = (total/100)*(100-discount);
}
但是我想把 staffMember 的名字写下来,这样我就可以让不同的 staffMembers 有不同的折扣。我做了这行不通
string employee;
cout << "Enter name: ";
cin >> employee;
cashy.applyStaffDiscount(employee);
方法:
void cashRegister::applyStaffDiscount(string employee){
total = (total/100)*(100-employee.getDiscountPercent());
}
谢谢汤姆
最佳答案
参数 employee
是一个 std::string
,而不是一个 staffMember
。在您的 applyStaffDiscount 函数中,您必须传递一个员工,而不是一个字符串:
string employee_name;
int employee_discount;
cout << "Enter employee data: ";
cin >> employee_name;
cin >> employee_discount;
staffMember employee( employee_name , employee_discount); //Staff variable
cashy.applyStaffDiscount(employee);
/* ... */
void cashRegister::applyStaffDiscount(const staffMember& employee)
{
total = (total/100)*(100-employee.getDiscountPercent());
}
关于c++ - 卡在访问方法 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17991606/