因此,我要将自定义语言解释器从Java转换为C ++,并且无法设置Token类型的currentToken成员。
tokens.hpp
#include <iostream>
class Token {
public:
std::string type;
Token(std::string newType);
};
class IntegerToken:public Token {
public:
int value;
IntegerToken(int newValue):Token("INTEGER") {
value = newValue;
};
};
class MinusToken:public Token {
public:
std::string value;
MinusToken():Token("OP") {
value = "-";
};
};
class PlusToken:public Token {
public:
std::string value;
PlusToken():Token("OP") {
value = "+";
};
};
class EOLToken:public Token {
public:
EOLToken():Token("NEWLINE"){};
};
tokens.cpp
#include "tokens.hpp"
Token::Token(std::string newType) {
type = newType;
}
int main() {
return 0;
}
BeanLexxer.hpp
#include <iostream>
#include "tokens.hpp"
class BeanLexxer {
std::string line;
int pos;
Token currentToken;
char currentChar;
bool isSpace(char c);
bool isDigit(char c);
void advance();
void skipWhitespace();
int getInteger();
void eat();
Token getNextToken();
void run();
public:
BeanLexxer(std::string newLine);
};
BeanLexxer.cpp
#include <iostream>
#include "BeanLexxer.hpp"
bool BeanLexxer::isSpace(char c) {
return c == ' ';
}
bool BeanLexxer::isDigit(char c) {
return (c > 64 & c < 91) || (c > 96 && c < 123);
}
void BeanLexxer::advance() {
pos++;
if (pos > line.length() - 1) {
currentChar = 0;
} else {
currentChar = line[pos];
}
}
void BeanLexxer::skipWhitespace() {
while (currentChar != 0 && isSpace(currentChar)) {
advance();
}
}
int BeanLexxer::getInteger() {
std::string result = "";
while (currentChar != 0 && isDigit(currentChar)) {
result += currentChar;
advance();
}
return std::stoi(result);
}
void BeanLexxer::eat() {
currentToken = getNextToken();
}
Token BeanLexxer::getNextToken() {
while (currentChar != 0) {
if (currentChar == ' ') {
skipWhitespace();
continue;
}
if (isDigit(currentChar)) {
return IntegerToken(getInteger());
}
if (currentChar == '+') {
advance();
return PlusToken();
}
if (currentChar == '-') {
advance();
return MinusToken();
}
}
return EOLToken();
}
void BeanLexxer::run() {
eat();
while ((currentToken.type).compare("NEWLINE") != 0) {
std::cout << currentToken.type << "\n";
eat();
}
std::cout << currentToken.type << "\n";
}
BeanLexxer::BeanLexxer(std::string newLine) {
line = newLine;
pos = 0;
currentChar = line[pos];
currentToken = EOLToken();
}
int main() {
return 0;
}
Runner程序(BeanInterpreter.cpp)
#include <iostream>
#include "BeanLexxer.hpp"
int main() {
BeanLexxer lexxer = BeanLexxer("3+45-6");
return 0;
}
Geany说该错误出在BeanLexxer.cpp的构造函数中,我必须显式初始化currentToken。我知道这意味着令牌currentToken(“ NULL”);,我认为可以做的唯一地方是.hpp,但这也带来了错误。它与我的类继承设置有关系吗,或者我还缺少其他东西? 错误:
g++ -Wall -o "BeanLexxer" "BeanLexxer.cpp" (in directory: /Users/Splavacado100/Desktop/Coding/VAULT/Bean-C++)
BeanLexxer.cpp:74:13: error: constructor for 'BeanLexxer' must explicitly initialize the member 'currentToken' which does not have a default constructor
BeanLexxer::BeanLexxer(std::string newLine) {
^
./BeanLexxer.hpp:8:8: note: member is declared here
Token currentToken;
^
./tokens.hpp:3:7: note: 'Token' declared here
class Token {
^
1 error generated.
Compilation failed.