C++ Switch 语句 [英] C++ Switch Statement

查看:35
本文介绍了C++ Switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近(昨天哈哈)我开始学习 C++.我正在尝试制作一个简单的计算器来练习.我使用了一个 switch 语句来调用类中的正确方法(或者它是函数......不知道 c++ 的细微差别......);

但是,代码不会编译,因为我使用一个字符串来定义要使用的情况,并且还定义了多个类以获得相同的结果.

Recently (yesterday hah) I started learning C++. I am trying to make a simple calculator to practice. I used a switch statement in order to call upon the correct method(or is it function.. don't know the nuances of c++...) within the class;

However, the code will not compile because I am using a string to define which case to use and also defining multiple classes to have the same result.

这是 switch 语句(在添加其他错误之前,我只做了添加以消除任何错误):

Here is the switch statement (I've only done addition to squash any bugs before I add the others):

switch(input){
        case 'A': case 'a': case 'add': case 'Add':
            cout << bo.addNum();
            break;
        default:
            cout << "Not addition";
            break;
    }

我得到的错误如下:

basic.cpp:41:2: error: statement requires expression of integer type ('string'
      (aka 'basic_string<char, char_traits<char>, allocator<char> >') invalid)
        switch(input){
        ^      ~~~~~
basic.cpp:42:28: warning: multi-character character constant [-Wmultichar]
                case 'A': case 'a': case 'add': case 'Add':
                                         ^
basic.cpp:42:40: warning: multi-character character constant [-Wmultichar]
                case 'A': case 'a': case 'add': case 'Add':
                                                     ^

这是它的全部代码:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class beckahClass{
public:
    void setNum(int num1, int num2){
        a = num1;
        b = num2;
    }
    int addNum(){
        return a + b;
    }
    int subNum(){
        return a - b;
    }
    int divNum(){
        return a / b;
    }
    int multNum(){
        return a * b;
    }
private:
    int a, b;
};

int main (void){
    beckahClass bo;
    string input;
    int a, b;

    cout << "Please specify the operation to preform by the following:\n A: add\nS: subtract\nM: Multiple\nD: divide\nEnter operation:  ";
    cin >> input;

    cout << "Enter the two nums you want to preform your operation on: ";
    cin >> a >> b;
    bo.setNum(a, b);

    switch(input){
        case 'A': case 'a': case 'add': case 'Add':
            cout << bo.addNum();
            break;
        default:
            cout << "Not addition";
            break;
    }   
    return 0;
}

我也意识到可能有更有效的逻辑使用方法;因为我刚刚开始使用 C++,我将不胜感激任何批评.之前我一直在简单地查找地图",想知道这是否是使用它的好实例?

I also realize there are probably more efficient ways to use logic; because I'm just starting C++, I would greatly appreciate any critique. I had been looking up 'maps' before briefly and was wondering if this might be a good instance to use it in?

谢谢.

推荐答案

原因是 C/C++ switch 语句采用 int 参数,不支持 string 作为类型.虽然它支持常量数组的概念.还要提到的是,C/C++ switch 语句通常生成为分支表.strong> 并且使用 string 样式开关生成分支表并不容易.

The reason is that C/C++ switch statement takes an int argument and do not support string as a type. Although it supports the notion of constant array. Also to mention that C/C++ switch statements are typically generated as branch tables. and it is not easy to generate a branch table using a string style switch.

在 C++ 中,switch 语句将 int 作为参数.

In C++ the switch statement takes int as the argument.

为什么你不能在 switch 中输入字符串并得到以下错误?

basic.cpp:42:28: warning: multi-character character constant [-Wmultichar]
                case 'A': case 'a': case 'add': case 'Add':
                                         ^
basic.cpp:42:40: warning: multi-character character constant [-Wmultichar]
                case 'A': case 'a': case 'add': case 'Add':

原因是为了生成 switch 的代码,编译器必须理解两个值相等的含义.对于 int 和 enum,因为它们是常量值,所以很简单.但是当涉及到字符串时,就很难导致错误.

The reason is because to generate the code for switch the compiler must understand what it means for two values to be equal. For int and enum, it is trivial and easy as they are constant values. But when it comes to string then it is difficult resulting in error.

这篇关于C++ Switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆