0

I currently have a base class Expr and some derivatives like AddExpr, MultExpr and NumExpr. I am trying to overload a general print() function that recursively calls the print() function of its members which are pointers to other Expr objects.

//Addition Expression
AddExpr::AddExpr( Expr* l, Expr* r ) {
    left = l;
    right = r;
}
string AddExpr::print() {
    string s;
    stringstream out;
    out << "(+" << left->print() << right->print() << ")";
    s = out.str();
}

NumExpr::NumExpr( string n ) {
    number = atoi( n.c_str() );
}
string NumExpr::print() {
    string s;
    stringstream out;
    out << number;
    s = out.str();
    return s;
}

So ultimately I want the add expression to print out a ( number + number ) if the left and right pointers are numbers, ( expression + number ), ( expression + expression ), etc. by recursively calling the print() function.

However I don't think I'm approaching this correctly as I am getting jumbled output :( I am not too familiar with pass by pointer and pass by reference, however every post that I have gone through with similar question are not quite relevant to my situation.

Here's a debug sample for calling the functions:

NumExpr* left = new NumExpr("1");
cout << left->print() << endl;
NumExpr* right = new NumExpr("2");
cout << right->print() << endl;
AddExpr* temp = new AddExpr( left, right );
cout << temp->print() << endl;
Expr* temp2 = temp;
cout << temp2->print() << endl; 

This will print out 1 and 2 but has the problem on the AddExpr.

Any help is appreciated!

EDIT: the header for my expression classes:

class Expr {
    public:
        virtual string name();
        virtual string print();
};

class AddExpr : public Expr {
    public:
        AddExpr(Expr* l, Expr* r);
        string print();
    private:
        Expr* left;
        Expr* right;
};

class NumExpr : public Expr {
    public:
        NumExpr( string v );
        string print();
    private:
        int number;
};
2
  • Could you please edit your question and add the complete source code of the relevant classes (Expr, AddExpr, NumExpr)? Also, what is the actual output of your sample, and what did you expect instead? Commented May 28, 2012 at 17:42
  • 2
    You forgot to return s; in AddExpr::print() Commented May 28, 2012 at 17:43

2 Answers 2

4

AddExpr::print() does not return any value.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This was the problem, the print() function is now correctly outputting my expressions. Much appreciated, though I would have hoped g++ would atleast warn me about something like this
@Jason: It does, but you have to ask it to enable warnings. I usually use -Wall -Wextra (and also -Werror to turn them into errors, so no-one can ignore them).
@MikeSeymour Thanks that's an awesome tip, I usually code in visualstudio on windows side, but unfortunately I don't have access to that at the moment, so I'm doing it via g++ and vim on linux side :) Good experience though.
2

I see two problems with your AddExpr::print() method:

  1. It doesn't actually return anything. You should have at least gotten a compiler warning, if not an error, for this.

  2. It doesn't put any space between left and right.

Try this:

string AddExpr::print() {
    string s;
    stringstream out;
    out << "(+" << left->print() << " " << right->print() << ")";
    s = out.str();
    return s;
}

Though actually, it would be better to do the following -- no need to create an explicit string variable:

string AddExpr::print() {
    stringstream out;
    out << "(+" << left->print() << " " << right->print() << ")";
    return out.str();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.