调用求和或获取乘积的函数不正确
The function being called to sum or get product is incorrect
当我运行这个函数时,总和或乘积是严重错误的。我输入了 2 和 3,得到了负一百万。产品也是如此。在我询问他们想要进行哪种计算以使其更自然之后,我将添加另一个 cout 语句。
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
class basicCalculator {
public:
int add(int x,int y) {
return add1 + add2;
}
int multiply(int x,int y) {
return multiply1*multiply2;
}
private:
int add1;
int add2;
int multiply1;
int multiply2;
};
int main() {
cout << "What mathematical action do you want?" << endl;
cout << "Press '1' to add two numbers, '2' to multiply two numbers" << endl;
int method;
cin >> method;
int value1;
cin >> value1;
int value2;
cin >> value2;
basicCalculator bc;
switch (method) {
case 1:
cout << "The sum is " << bc.add(value1, value2) << endl;
break;
case 2:
cout << "The product is " << bc.multiply(value1, value2) << endl;
}
}
在add
和multiply
方法中,您使用的是(非初始(成员变量,而不是实际的参数。
尝试:
int add(int x, int y) { return x + y; }
建议:add
和multiply
根本不需要对象状态,它们可能是静态的,我看不出您声明的所有成员变量有任何理由。
Freddy,在你的函数中,你必须使用命名参数进行计算。所以,不是
int add(int x,int y) {
return add1 + add2;
}
而是
int add(int x,int y) {
return x + y;
}
乘法也有同样的问题。
相关文章:
- g++ 说函数不存在,即使包含正确的标头
- 将参数传递给泛型 lambda 时复制构造函数不正确
- 非静态成员函数的 decltype 格式不正确吗?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 编译器在构造函数中计算的成员偏移量不正确
- 为什么在template函数广播中把两个extensor表达式加在一起不正确
- 从模板化类虚拟函数中调用的模板函数不正确
- 为什么不调用预期的函数?我是否对类型特征的理解不正确?
- 将函数的地址转换为UINTPTR_T给我不正确的结果
- 模板实例化失败:编译器选择不正确的重载函数
- 为什么构造函数C++接受不正确的类型作为参数?
- 不正确的输出和变量未用Eclipse CDT初始化构造函数
- 不正确的成员构造函数定义
- 调用求和或获取乘积的函数不正确
- 当const在另一行处的const时,doxygen parse const成员函数不正确
- 模板中调用的虚拟函数不正确
- C++将数组传递给函数不正确
- 构造函数不正确和隐式删除
- c++ If, else If不读else If.或发现函数不正确