Reputation: 325
I have the following code:
// FILE_NAME: compiler.h
typedef enum Precedence { /* ENUM MEMBERS */ } Precedence;
typedef enum TokenType { /* ENUM MEMBERS */ } TokenType;
typedef void (Compiler::*ParseFn)();
typedef struct ParseRule {
ParseFn prefix;
ParseFn infix;
Precedence precedence;
} ParseRule;
class Compiler {
public:
std::unordered_map<TokenType, ParseRule> rules;
ParseRule getRule(TokenType type) {
return rules[type];
};
void binary();
void unary();
};
I am trying to define a hashmap that maps a key of TokenType
to a value that has the form of the struct ParserRule
.
An example of a key-value pair for the Compiler::rules
hashmap is as follows:
rules[TOKEN_MINUS] = {unary, binary, PREC_TERM}
When I try to compile with C++11, I get the following error, plus many others:
./compiler/compiler.h:58:15: error: use of undeclared identifier 'Compiler'
typedef void (Compiler::*ParseFn)();
This makes sense since I am trying to use the class Compiler
to define the type ParseFn
before the Compiler
class has been defined.
I am very new to coding in C/C++ (I started learning a couple of weeks ago), so I was wondering if there is another way of defining the types and classes without causing a compiler error.
Upvotes: 3
Views: 51
Reputation: 182000
You need to (pre-)declare Compiler
as a class before referring to it:
class Compiler;
typedef void (Compiler::*ParseFn)();
Upvotes: 3