Leon C. 2019-12-26 17:15 采纳率: 0%
浏览 813
已采纳

c++ 这个报错是什么原因?如何解决?

以下代码在编译时报错:

#include<iostream>
using namespace std;
string type_get, str_empty, str_num1, str_num2;
string to_do = "+-*/";
int empty, i, operator_num, num1, num2;
int flag = 1;//flag=1:运行,flag=0:退出

int plus(int num1,int num2)
{
    int result;
    result = num1 + num2;
    return result;
}

int minus(int num1,int num2)
{
    int result;
    result = num1 - num2;
    return result;
}

int multiply(int num1,int num2)
{
    int result;
    result = num1 * num2;
    return result;
}

int divide(int num1,int num2)
{
    int result;
    result = num1 / num2;
    return result;
}

int main()
{
    while(flag)
    {
        cin >> type_get;

        if (type_get == "exit")
        {
            flag = 0;
        }
        else
        {
            operator_num = type_get.find_first_of(to_do);

            char array_type_get[type_get.length()];

            for (i = 0; i < type_get.length(); i++)
            {
                array_type_get[i] = type_get[i];
            }


            for (i = 0; i < operator_num; i++)
            {
                str_num1[i] = array_type_get[i];
            }

            for (i = operator_num + 1; i < sizeof(array_type_get); i++)
            {
                str_num2[i] = array_type_get[i];
            }

            num1 = atoi(str_num1.c_str());
            num2 = atoi(str_num2.c_str());

            switch (array_type_get[operator_num])
            {
                case "+" :
                    plus(num1,num2);
                    break;

                case "-" :
                    minus(num1,num2);
                    break;

                case "*" :
                    multiply(num1,num2);
                    break;

                case "/" :
                    divide(num1,num2);
                    break;

                default :
                    cout << "请输入一个运算符!" << endl;
                    break;
            }
        }
    }

    return 0;
}

报错信息如下:

calculator.cpp: In function 'int main()':
calculator.cpp:73:10: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
     case "+" :
          ^~~
calculator.cpp:73:10: error: conversion from pointer type 'const char (*)[2]' to arithmetic type 'int' in a constant expression
calculator.cpp:74:6: error: reference to 'plus' is ambiguous
      plus(num1,num2);
      ^~~~
In file included from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\string:48,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\locale_classes.h:40,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\ios_base.h:41,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:42,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
                 from calculator.cpp:1:
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\stl_function.h:147:12: note: candidates are: 'template<class _Tp> struct std::plus'
     struct plus;
            ^~~~
calculator.cpp:8:5: note:                 'int plus(int, int)'
 int plus(int num1,int num2)
     ^~~~
calculator.cpp:77:10: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
     case "-" :
          ^~~
calculator.cpp:77:10: error: conversion from pointer type 'const char (*)[2]' to arithmetic type 'int' in a constant expression
calculator.cpp:78:6: error: reference to 'minus' is ambiguous
      minus(num1,num2);
      ^~~~~
In file included from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\string:48,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\locale_classes.h:40,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\ios_base.h:41,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:42,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
                 from calculator.cpp:1:
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\stl_function.h:150:12: note: candidates are: 'template<class _Tp> struct std::minus'
     struct minus;
            ^~~~~
calculator.cpp:15:5: note:                 'int minus(int, int)'
 int minus(int num1,int num2)
     ^~~~~
calculator.cpp:81:10: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
     case "*" :
          ^~~
calculator.cpp:81:10: error: conversion from pointer type 'const char (*)[2]' to arithmetic type 'int' in a constant expression
calculator.cpp:85:10: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
     case "/" :
          ^~~
calculator.cpp:85:10: error: conversion from pointer type 'const char (*)[2]' to arithmetic type 'int' in a constant expression

在这上面卡了好几天了,求各位大神解答!

  • 写回答

2条回答 默认 最新

  • qtchen_1988 2019-12-26 18:13
    关注
    #include<iostream>
    #include <stdlib.h>
    
    using namespace std;
    string type_get, str_empty, str_num1, str_num2;
    string to_do = "+-*/";
    int empty, i, operator_num, num1, num2;
    int flag = 1;//flag=1:运行,flag=0:退出
    
    int myplus(int num1,int num2)
    {
        int result;
        result = num1 + num2;
        return result;
    }
    
    int myminus(int num1,int num2)
    {
        int result;
        result = num1 - num2;
        return result;
    }
    
    int multiply(int num1,int num2)
    {
        int result;
        result = num1 * num2;
        return result;
    }
    
    int divide(int num1,int num2)
    {
        int result;
        result = num1 / num2;
        return result;
    }
    
    int main()
    {
        while(flag)
        {
            cin >> type_get;
    
            if (type_get == "exit")
            {
                flag = 0;
            }
            else
            {
                operator_num = type_get.find_first_of(to_do);
    
                char array_type_get[type_get.length()];
    
                for (i = 0; i < type_get.length(); i++)
                {
                    array_type_get[i] = type_get[i];
                }
    
    
                for (i = 0; i < operator_num; i++)
                {
                    str_num1[i] = array_type_get[i];
                }
    
                for (i = operator_num + 1; i < sizeof(array_type_get); i++)
                {
                    str_num2[i] = array_type_get[i];
                }
    
                num1 = atoi(str_num1.c_str());
                num2 = atoi(str_num2.c_str());
    
                switch (array_type_get[operator_num])
                {
                case '+' :
                    myplus(num1,num2);
                    break;
    
                case '-' :
                    myminus(num1,num2);
                    break;
    
                case '*' :
                    multiply(num1,num2);
                    break;
    
                case '/' :
                    divide(num1,num2);
                    break;
    
                default :
                    cout << "请输入一个运算符!" << endl;
                    break;
                }
            }
        }
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。