Xavier_NZX 2017-06-01 12:15 采纳率: 100%
浏览 841
已采纳

菜鸟的C++出错了,求解答

#include "Calculator.h"
#include
#include

Calculator::Calculator(double i, double j, char *prt) {
operand1 = i;
operand1 = j;
operation = new char[20];
strcpy_s(operation, 10, prt);
};

double Calculator::doOperation() {
if (strcmp(operation, "div") == 0) {
if (operand2 == 0) {
std::cout << "operand2 can't be 0" << std::endl;
return 1;
}
return operand1 / operand2;
}
else if(strcmp(operation, "minus") == 0)
return operand1 - operand2;
else if(strcmp(operation, "multi") == 0)
return operand1 * operand2;
else if(strcmp(operation, "plus") == 0)
return operand1 + operand2;
};

using namespace std;
int main(int argc, char *argv[]) {

if (argc != 4) {

    cout << "You must give 4 arguments!" << endl;

    return 1;
}

double op1, op2,m;

char *opcode;

op1 = atof(argv[2]);

op2 = atof(argv[3]);

opcode = argv[1];

Calculator one(op1, op2, opcode);

m = one.doOperation();

cout << m << endl;

return 0;

}
图片说明
include的部分显示不出来,其实是没问题的,这个编辑器真难用

  • 写回答

2条回答 默认 最新

  • 战在春秋 2017-06-02 00:48
    关注

    在构造函数中operand2没有接收到值。

    Calculator::Calculator(double i, double j, char *prt) {
    operand1 = i;  //这里是operand1
    operand1 = j;  //这里也是operand1
    operation = new char[20];
    strcpy_s(operation, 10, prt);
    };
    

    类似这样的错误,编译器不会报,小心一些就好了。

    用心回答每个问题,如果有帮助,请采纳答案好吗,谢谢~~~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型