简体   繁体   中英

c++ having trouble calling a static function from another classes main function

I feel like I should be able to use Parser::toString , but

I have the class silc.cc here:

#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***
}

And I get the error 'toString' is not a member of 'Parser'

In parser.h , I have:

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

I am not sure if it is unable to find the toString() function in parser.h for some reason, or if I am using the wrong syntax, as I am pretty new to C++.

Edit: here is the entire parser.h class:

#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

From what I'm seeing, the toString and toString0 are both static methods of TreeNode and not Parser . Try calling them like so: Parser::TreeNode::toString .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM