【问题标题】:Why do I keep getting the error message "no member named "TokenType" in 'Token' '"?为什么我不断收到错误消息“'Token''中没有名为“TokenType”的成员”?
【发布时间】:2017-03-13 04:48:42
【问题描述】:

我正在使用 c++ 为我的一个类制作词法分析器,但我的头文件的内容包含在我的源文件中时遇到了问题。我将头文件#include 到我的源文件中,但它一直说找不到包含的成员。

头文件为:

#include <string>
#include <iostream>

enum TokenType {
    ID,         // identifier
    STR,        // string
    INT,        // integer constant
    PLUS,       // the + operator
    STAR,       // the * operator
    LEFTSQ,     // the [
    RIGHTSQ,    // the ]
    PRINT,      // print keyword
    SET,        // set keyword
    SC,     // semicolon
    LPAREN,     // The (
    RPAREN,     // the )
    DONE,       // finished!
    ERR,        // an unrecoverable error
};

class Token {
private:
    TokenType   tok;
    std::string lexeme;

public:
    Token() : tok(ERR), lexeme("") {}
    Token(TokenType t, std::string s) : tok(t), lexeme(s) {}

    TokenType   getTok() const      { return tok; }
    std::string getLexeme() const   { return lexeme; }

    friend bool operator==(const Token& left, const Token& right) {
        return left.tok == right.tok;
    }

    friend std::ostream& operator<<(std::ostream& out, const Token& t);
};

extern  int linenum;
extern  Token   getToken(std::istream* instream);

虽然我从有多个错误的文件之一中获得了这部分代码:

 map<Token::TokenType,int> tokenCount;
    map<string,int> lexemeCount;
    Token t;

    while( (t = getTok(br)) != Token::TokenType::DONE && t !=Token::TokenType::ERR ) {
        tokenCount[t.getType()]++;
        if( t == Token::TokenType::VAR
           || t == Token::TokenType::SCONST
           || t == Token::TokenType::ICONST ) {
            lexemeCount[t.getLexeme()]++;
        }

我确信这很容易解决,但我不确定如何解决。我这几天一直在尝试修复它,但无法弄清楚。

【问题讨论】:

  • 但是Token没有这样的会员!!!除了一堆成员函数之外,您在Token 中声明的唯一成员是toklexeme。你为什么要访问Token::TokenType

标签: c++ file header include


【解决方案1】:

您的错误正是调试器告诉您的。 TokenType 不是 Token 类的成员,所以不能说 Token::TokenType。您只需将其称为 TokenType。

【讨论】:

  • "你的错误正是调试器告诉你的。"代码不会到达调试器。你的意思是“编译器”吗?
  • 我猜你是对的,这是一个编译器错误,它来自编译器。但这不是我评论的重点,问题不是“这个错误来自哪里?”,所以我会说这是一个可以忽略不计的错误。
【解决方案2】:

您在Token 之外定义了TokenType,因此在Token 内部没有定义名为TokenType 的标识符,因此没有定义Token::TokenType

你有两个明显的选择。在Token 中定义TokenType

#include <string>
#include <iostream>

class Token {

    enum TokenType {
        ID,         // identifier
        // ...
        ERR,        // an unrecoverable error
    };

// ...    
};

...或者使用名称 TokenTypewithout theToken::on the front. If you want to qualify the name withTokenType, use anenum class`:

enum class TokenType {
    ID,         // identifier
    // ...
    ERR,        // an unrecoverable error
};

map<TokenType, int> tokenCount;
map<string, int> lexemeCount;
Token t;

while ((t = getTok(br)) != TokenType::DONE && t != TokenType::ERR) {
    tokenCount[t.getType()]++;
    if (t == TokenType::VAR
        || t == TokenType::SCONST
        || t == TokenType::ICONST) {
        lexemeCount[t.getLexeme()]++;
    }

当然,您可以组合类型,并将TokenType 定义为enum class 内的Token 类以及(尽管有些人会认为这有点过度保护,可以这么说)。

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 1970-01-01
    • 2017-01-25
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多