首页 > 解决方案 > 当我有一个与当前类中使用的值相同的枚举时,如何使用另一个类的枚举?C++

问题描述

我无法返回一个要求我从头等舱返回枚举“BEGIN”的函数。我遇到的问题是我正在使用的当前类中使用单独的枚举列表。因此,当我返回函数时,它是从当前类而不是派生类中获取的。有人知道怎么修这个东西吗?头类:

enum Token {
    PRINT, BEGIN, END, IF, THEN}
LexItem(Token token, string lexeme, int line) {
        this->token = token;
        this->lexeme = lexeme;
        this->lnum = line;
    }

函数类:

LexItem getNextToken(istream& in, int& linenum){
enum TokState { BEGIN, INID, INSTRING, ININT, INREAL, INCOMMENT, SIGN }
if(lexeme.compare("begin")){
   LexItem item = LexItem(BEGIN, lexeme, linenum); //error on LexItem no instance of constructor
   return item;}

我认为这是因为它使用 TokState begin 而不是来自 Token 的。不知道如何修复。我无法更改任一类中的枚举。

标签: c++enums

解决方案


推荐阅读