Search code examples
c++classprivate-members

Using a private member function in the class


So I have this class:

The purpose of this class is to change the post fix algebraic expression (1234*+-) to infix expression (1*2+3-4).

    class PostfixExpression
    {
    private:
        string postfix;
        vector<string> tokenizedEx;
        double result;

        void tokenizeStr(); // Where do I call this?

    public:
        PostfixExpression(string p);    
        //mutators, and accessors for string and double only
        //no accessor and mutator for vectors
        string changeToInfix() const;
    };
    PostfixExpression::PostfixExpression(string p)
    {
        setPost(p);
    }
   //mutators, and accessors
   void PostfixExpression::tokenizeStr() 
    {

        stringstream ss(postfix);
        tokenizedEx.clear();
        string hold;
        int i = 0;
        while (ss >> hold)
        {
            tokenizedEx.push_back(hold);
        }
    }
    //....

The purpose of the private class tokenizeStr() is to tokenizing the string and put it into the vector<string> tokenizedEx.

For example, in my main, I would have

int main()
{
     PostfixExpression test("1 2 3 4 * + -");
}

Now I am trying to tokenize the string and update it to the vector<string> tokenizedEx via the private member function tokenizeStr().

After tokenized, each element in the vector should either contain an integer or an operator, but I can't seem to find a way to call the function.

I know it is totally illegal to call private member functions from main since the function is private.

Any suggestions is appreciated.


Solution

  • I see a changeToInfix() method in your class which you haven't shown the implementation of. That method should be the one calling tokenizeStr().