【发布时间】: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中声明的唯一成员是tok和lexeme。你为什么要访问Token::TokenType?