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条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。