访问方法C++时出现问题
stuck accessing a method C++
我正在尝试重新创建一个收银机程序。我有一个员工对象,它有一个名称和折扣%
staffMember me ("tom", 20);
我想把折扣加到收银台的总额上。如果我像这样使用me.getDiscountPercent将折扣作为整数参数传递,我的方法就会起作用
cashy.applyStaffDiscount(me.getDiscountPercent());
void cashRegister::applyStaffDiscount(int discount){
total = (total/100)*(100-discount);
}
然而,我想记住员工的名字,这样我就可以有不同折扣的员工。我做了这个不起作用
string employee;
cout << "Enter name: ";
cin >> employee;
cashy.applyStaffDiscount(employee);
方法:
void cashRegister::applyStaffDiscount(string employee){
total = (total/100)*(100-employee.getDiscountPercent());
}
感谢Tom
参数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());
}
employee
的数据类型为字符串。string
类(来自std
命名空间)没有任何名称为getDiscountPercent()
的方法。也许,你想做的是:
string name;
int discount;
cout << "Enter name: ";
cin >> name;
cout << "Enter discount: ";
cin >> discount;
staffMember employee (name, discount); // <-- this is what you really want!
cashy.applyStaffDiscount(employee);
相关文章:
- 最小硬币更换问题(自上而下方法)
- 使用同一类中的方法重新分配字段时出现问题
- 我想直接在结构中插入,但没有一种方法可以正确避免填充问题
- Protobuf中重复字段的问题.使用重复字段进行序列化/反序列化的更好方法是什么?
- 此动态编程问题的自上而下方法
- 此解决方案中生成更改的方法数量(自上而下)有什么问题?
- 使用模板方法为标头提供空 cpp 时出现问题
- 这种针对 N Queen 问题的回溯方法不正确吗?
- C++ push() 和 pop() 方法使用指针的动态 LinkedList 的问题
- CreateFileA方法出现问题
- 必须使用尾随返回类型的示例,因为无法用旧方法解决问题
- 双重检查创建单例问题的方法
- C++ 指针问题 - 通过方法更新指针
- C++隐式/显式模板方法专用化问题
- 迭代快速排序方法的分区算法问题
- 单独的类声明和方法定义文件问题
- 我通过迭代加法将二进制数转换为十进制并检查单个字符(请参阅代码)的方法有什么问题?
- 如何通过动态规划方法解决这个问题?
- C++ 03 类模板 这是转换构造函数还是转换运算符?以及如何声明解决此问题的方法
- 一个c++语法问题:方法的类