gpt4 book ai didi

c++无法从另一个类主函数调用静态函数

转载 作者:太空狗 更新时间:2023-10-29 20:22:20 26 4
gpt4 key购买 nike

我觉得我应该可以使用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 member of 'Parser'

parser.h 中,我有:

class Parser {
public:
static string toString(TreeNode *node) {
return toString0(node, 0);
}
}

我不确定是否由于某种原因无法在 parser.h 中找到 toString() 函数,或者我是否使用了错误的语法,如我是 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

最佳答案

据我所知,toStringtoString0 都是 TreeNode 的静态方法,而不是 Parser。尝试这样调用它们:Parser::TreeNode::toString

关于c++无法从另一个类主函数调用静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38190196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com