2016-07-04 68 views
3

我覺得我應該能夠使用Parser::toString靜態函數,但C++有麻煩調用從另一個班主要功能

我有類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); 
    } 
} 

我不確定它是否由於某種原因無法在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 
+2

在'Parser'類/命名空間裏是'TreeNode'嗎? –

+0

這是正確的語法。還有其他的事情可能會由其他錯誤引發。有沒有其他信息可以提供? – Curious

+0

我可以給整個parser.h類。它確實定義了TreeNode。 –

回答

6

從我所看到的,在toStringtoString0TreeNode而不是Parser靜態方法。嘗試像這樣調用它們:Parser::TreeNode::toString