I'm working on a compiler for a toy language and I created a struct to hold the result of the lexing step. Now I want to create a struct to hold the result from the parser so I thought I would create a base struct since they both hold warnings and error but one has tokens and one has statements. This is what my structs look like.
struct Result {
std::vector<Warning> warnings;
std::vector<Error> errors;
void add(Warning warning) { warnings.push_back(warning); }
void add(Error error) { errors.push_back(error); }
bool hasWarnings() { return warnings.size() > 0; }
bool hasErrors() { return errors.size() > 0; }
};
struct LexerResult : public Result {
std::vector<Token> tokens;
void add(Token token) { tokens.push_back(token); }
};
In the part of the code where I was adding errors it looked like this.
result.add(Error { loc, "Unknown token" });
But after the change to the structs I get this error.
moo_compiler/lexer/lexer.cpp:272:36 No viable conversion from 'Warning' to 'Token'
When I rely on the code autocomplete it wants this.
result.Result::add(Error { loc, "Unknown token" });
but I can't fathom why.