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

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

c++

2个回答

#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;
}

'+',要用单引号,因为是char,不是字符串
下面类似

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:退出
这几行放在main里面

plus
minus
这些函数改名,因为这些名字函数库已经使用了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问