我觉得我应该可以使用Parser::toString
,但
我这里有班级silc.cc
:
#include "Token.h"
#include "Lexer.h"
#include "parser.h"
#include <iostream>
#include <fstream>
using namespace std;
void work(istream& input);
int main(int argc, char **argv){
if(argc > 1){
ifstream in;
in.open(argv[1]);
work(in);
in.close();
}
else{
work(cin);
}
return 0;
}
void work(istream& input)
{
Lexer lex(input);
Parser par(lex, cout);
Parser::TreeNode* node = par.program();
cout << Parser::toString(node) << endl; //error is here***
}
我收到错误'toString' is not a member of 'Parser'
在parser.h
中,我有:
class Parser {
public:
static string toString(TreeNode *node) {
return toString0(node, 0);
}
}
我不确定它是否由于某种原因无法在toString()
中找到parser.h
函数,或者我使用了错误的语法,因为我对C ++很陌生。
编辑:这是整个parser.h类:
#ifndef PARSER_H
#define PARSER_H
#include "Token.h"
#include "Lexer.h"
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <sstream>
using namespace std;
class Parser {
private:
enum Operation {
ADD, SUB, AND, DIV, REM, ISEQ, ISGE, ISGT, ISLE, ISLT,
ISNE, MULT, OR,
LOADL, LOADV, STOREV, JUMPF, JUMP, INSLABEL,
PRINT, SEQ, PRINTLN
};
public:
class TreeNode {
public:
Operation op;
string val;
TreeNode *leftChild;
TreeNode *rightChild;
void init(Operation opx, string valx, TreeNode *leftChildx, TreeNode *rightChildx) {
op = opx;
val = valx;
leftChild = leftChildx;
rightChild = rightChildx;
}
TreeNode(Operation op, string val) {
init(op, val, NULL, NULL);
}
TreeNode(Operation op, string val, TreeNode *leftChild, TreeNode *rightChild) {
init(op, val, leftChild, rightChild);
}
TreeNode(Operation op) {
init(op, "", NULL, NULL);
}
TreeNode(Operation op, TreeNode *leftChild, TreeNode *rightChild) {
init(op, "", leftChild, rightChild);
}
static string toString(TreeNode *node) {
return toString0(node, 0);
}
static string toString0(TreeNode *node, int spaces) {
static string blanks = " ";
string left = "";
string right = "";
bool isLeaf = true;
if (node->leftChild != NULL) {
left = toString0(node->leftChild, spaces+2);
isLeaf = false;
}
if (node->rightChild != NULL) {
right = toString0(node->rightChild, spaces+2);
isLeaf = false;
}
string ret;
if (isLeaf) {
ret = blanks.substr(0, spaces) + ops[node->op] + "[" + node->val + "]";
} else {
ret = blanks.substr(0, spaces) + ops[node->op] + "(\n" + left + ",\n" + right + "\n" +
blanks.substr(0, spaces) + ")";
}
return ret;
}
};
private:
Lexer lexer;
Token token;
ostream& out;
int lindex;
int tindex;
string itos(int i) { stringstream ss; ss << i; string res = ss.str(); return res;}
string makeLabel() { string tmp = "L"; stringstream ss; ss << ++lindex; string res = ss.str(); tmp = tmp + res; return tmp;}
static const string ops[];
void error(string message);
void check(int tokenType, string message);
public:
TreeNode *program();
TreeNode* compoundStatement();
TreeNode* statement();
TreeNode* setStatement();
TreeNode* printStatement();
TreeNode* whileStatement();
TreeNode* ifStatement();
TreeNode* switchStatement();
TreeNode* logicalExpression();
TreeNode* relationalExpression();
TreeNode* expression();
TreeNode* term();
TreeNode* factor();
Parser(Lexer& lexer, ostream& out);
~Parser();
};
#endif
答案 0 :(得分:6)
从我看到的情况来看,toString
和toString0
都是TreeNode
而非Parser
的静态方法。尝试像这样调用它们:Parser::TreeNode::toString
。