从char的C ++枚举 [英] C++ enum from char

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

问题描述

好吧,我是C ++的新人。我得到了Bjarne的书,我试图遵循计算器代码。



但是,编译器吐出一个关于这一部分的错误:

  token_value get_token()
{
char ch;

do {//跳过空格除了'\\\
'
if(!std :: cin.get(ch))return curr_tok = END;
} while(ch!='\\\
'&& isspace(ch));

switch(ch){
case';':
case'\\\
':
std :: cin> WS; // skip whitespace
return curr_tok = PRINT;
case'*':
case'/':
case'+':
case' - ':
case'(':
case ')':
case'=':
return curr_tok = ch;
case'0':case'1':case'2':case'3':case'4':case'5':
case'6':case' 8':case'9':case'。':
std :: cin.putback(ch);
std :: cin>> number_value;
return curr_tok = NUM​​BER;
默认值:// NAME,NAME =或错误
if(isalpha(ch)){
char * p = name_string;
* p ++ = ch;
while(std :: cin.get(ch)&& isalnum(ch))* p ++ = ch;
std :: cin.putback(ch);
* p = 0;
return curr_tok = NAME;
}
错误(bad token);
return curr_tok = PRINT;
}

它吐出的错误是:

  calc.cpp:42:错误:从'char'到'token_value'无效转换

token_value 是一个枚举,如下所示:

  enum token_value {
NAME,NUMBER,END,
PLUS ='+',MINUS =' - ',MUL ='*',DIV ='/',
PRINT =';',ASSIGN ='=',LP ='(',RP =')'
};
token_value curr_tok;

我的问题是,如何将ch(从cin)转换为相关的枚举值? / p>

解决方案

您不能从 char $ c> enum - 你必须明确地做:

  return curr_tok = static_cast< token_value> (ch)。 

但要小心!如果您的 enum 值没有匹配您的 char ,那么很难使用结果:)


Ok, I'm new at C++. I got Bjarne's book, and I'm trying to follow the calculator code.

However, the compiler is spitting out an error about this section:

token_value get_token()
{
    char ch;

    do {        // skip whitespace except '\n'
        if(!std::cin.get(ch)) return curr_tok = END;
    } while (ch!='\n' && isspace(ch));

    switch (ch) {
        case ';':
        case '\n':
            std::cin >> WS;      // skip whitespace
            return curr_tok=PRINT;
        case '*':
        case '/':
        case '+':
        case '-':
        case '(':
        case ')':
        case '=':
            return curr_tok=ch;
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9': case '.':
            std::cin.putback(ch);
            std::cin >> number_value;
            return curr_tok=NUMBER;
        default:            // NAME, NAME=, or error
            if (isalpha(ch)) {
                char* p = name_string;
                *p++ = ch;
                while (std::cin.get(ch) && isalnum(ch)) *p++ = ch;
                std::cin.putback(ch);
                *p = 0;
                return curr_tok=NAME;
            }
            error("bad token");
            return curr_tok=PRINT;
}

The error it's spitting out is this:

calc.cpp:42: error: invalid conversion from ‘char’ to ‘token_value’

token_value is an enum that looks like:

enum token_value {
    NAME,       NUMBER,     END,
    PLUS='+',   MINUS='-',  MUL='*',  DIV='/',
    PRINT=';',  ASSIGN='=', LP='(',   RP=')'
};
token_value curr_tok;

My question is, how do I convert ch (from cin), to the associated enum value?

解决方案

You can't implicitly cast from char to an enum - you have to do it explicitly:

return curr_tok = static_cast<token_value> (ch);

But be careful! If none of your enum values match your char, then it'll be hard to use the result :)

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

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