0

I am making an expression tree for a school assignment and am getting these error in my code. Being quite new to C++ I have searched the web for answers to no avail.

#include<iostream>
using namespace std;
#include "expression.h"


namespace Student
{
    
    void writeNode(NodeType* node);
    /*
    Displays an arithmetic expression.
    @pre The node is initialized.
    @post This node has been displayed.
    */

    int answer(NodeType* node);
    /*
    returns the value of the SUB_NODE, or the value
    if node is just an int.
    */
    
    void Expression::display() const
    {
        writeNode(eTree*);       //error here
    }

    int Expression::getValue() const
    {
        return answer(eTree*);   //error here
    }
    
    void writeNode(NodeType node)
    {
        if(node.tag == SUB_NODE)
        {
            cout<<"(";
            writeNode(node.left);
            cout<< node.op;
            writeNode(node.right);
            cout<<")";
        }
        else if(node.tag == INT_ONLY)
            cout<<node.intValue;
    }
    int answer(NodeType node)
    {
        if(node.tag == INT_ONLY)
            return node.intValue;
        else
        {
            switch(node.op)
            {
            case '+':
                return answer(node.left) + answer(node.right);
            case '-':
                return answer(node.left) - answer(node.right);
            case '*':
                return answer(node.left) * answer(node.right);
            case '/':
                return answer(node.left) / answer(node.right);
            }
        }
        
    }
}

The output build is;
1>c:\users\ben\documents\visual studio 2008\projects\test\test\expression1.cpp(34) : error C2059: syntax error : ')'
1>c:\users\ben\documents\visual studio 2008\projects\test\test\expression1.cpp(39) : error C2059: syntax error : ')'

Those are the errors I'm getting at the lines I've specified(lines don't line up to same on here). Any help you could give me would be greatly appreciated.

4
Contributors
16
Replies
17
Views
5 Years
Discussion Span
Last Post by bailsb
0

oh shoot, sorry

//expression.h

#ifndef EXPRESSION_H
#define EXPRESSION_H

namespace Student
{

enum TagType //For labeling data content in a binary expression tree node.
{
    INT_ONLY,
    SUB_NODE
};

struct NodeType //For nodes in a binary expression tree.
{
    TagType tag;
    union //Note that this union type is "anonymous".
    {
        int intValue;
        struct //And this struct type is also anonymous.
        {
            NodeType* left;
            char op;
            NodeType* right;
        };
    };
};

class Expression
{
public:
    bool Expression::isRead
    (
        istream& inStream //inout
    );
    /**<
    Determines if a valid expression can be read from inStream and, if so,
    reads and stores the expression.
    @param inStream The input stream from which the expression is read.
    @return true if an expression has been successfully read from inStream,
    otherwise false.
    @pre inStream has been initiallzed and is open for reading.
    @post inStream is still open for reading. An expression may or may not
    have been successfully read.
    */


    void display() const;
    /**<
    Displays an arithmetic expression. If the expression is a single
    non-negative integer, it simply displays that integer. Otherwise
    it displays the expression fully parenthesized, with a single
    blank space before, and a single blank space after, each operator.
    @pre This expression has been initialized.
    @post This expression has been displayed, as above, starting from
    the current cursor position. Note too that this method does not
    terminate the line on which the expression is displayed.
    */


    int getValue() const;
    /**<
    Returns the value of this expression.
    */

private:
    NodeType eTree;
};

} //End of this part of namespace Student

#endif

also, the isRead function is taken care of by my profs .obj file. So I don't have a written function for that

Edited by bailsb: n/a

0

Your signatures are inconsistant. Once you fix there will be other problems...

void writeNode(NodeType* node);
void writeNode(NodeType node)

Edited by template<>: n/a

0

ok, that has been fixed, but I'm still getting the same errors. The new code

#include<iostream>
using namespace std;
#include "expression.h"


namespace Student
{
    
    void writeNode(NodeType* node);
    /*
    Displays an arithmetic expression.
    @pre The node is initialized.
    @post This node has been displayed.
    */

    int answer(NodeType* node);
    /*
    returns the value of the SUB_NODE, or the value
    if node is just an int.
    */
    
    void Expression::display() const
    {
        writeNode(eTree*);    //error here
    }

    int Expression::getValue() const
    {
        return answer(eTree*);  //error here
    }
    
    void writeNode(NodeType* node)
    {
        if(node->tag == SUB_NODE)
        {
            cout<<"(";
            writeNode(node->left);
            cout<< node->op;
            writeNode(node->right);
            cout<<")";
        }
        else if(node->tag == INT_ONLY)
            cout<<node->intValue;
    }
    int answer(NodeType* node)
    {
        if(node->tag == INT_ONLY)
            return node->intValue;
        else
        {
            switch(node->op)
            {
            case '+':
                return answer(node->left) + answer(node->right);
            case '-':
                return answer(node->left) - answer(node->right);
            case '*':
                return answer(node->left) * answer(node->right);
            case '/':
                return answer(node->left) / answer(node->right);
            }
        }   
    }
}

Edited by bailsb: n/a

0

What is eTree??

void Expression::display() const
{
    writeNode(eTree*);    //error here
}

Edited by mike_2000_17: Fixed formatting

0

^^yes actually I do, but if i change that eTree is a const NodeType* while each recursive call will simply be NodeType*

0

try this and see what happens, then figure out how it should be fixed

struct node_t
{
    node_t *left;
    node_t *right;
};

void something(node_t *)
{
}

int main()
{
    node_t node;

    something(node*); // #1 what is this??
    something(&node); // #2 pass a pointer to node
}

Edited by template<>: n/a

0

Line 35 in class Expression, you are missing semicolon at the end.

istream& inStream //inout

Edited by Taywin: n/a

0

Also, just out of curiosity, why did you decide to use an anonymous struct below?

struct NodeType //For nodes in a binary expression tree.
{
    TagType tag;
    union //Note that this union type is "anonymous".
    {
        int intValue;
        struct //And this struct type is also anonymous.
        {
            NodeType* left;
            char op;
            NodeType* right;
        };
    };
};

Edited by template<>: n/a

0

that part was done by my prof. In all honesty, I'm not quite sure

Edited by bailsb: n/a

0

Ask him why when you get a chance, probably has a good reason, although anonymous struct is not part of the c++ standard to the best of my knowledge.

0

Will do, I have found a way that works. Thanks for your help. In case your curious this is my new code

#include<iostream>
using namespace std;
#include "expression.h"


namespace Student
{
    
    void writeNode(const NodeType* node);
    /*
    Displays an arithmetic expression.
    @pre The node is initialized.
    @post This node has been displayed.
    */

    int answer(const NodeType* node);
    /*
    returns the value of the SUB_NODE, or the value
    if node is just an int.
    */
    
    void Expression::display() const
    {
        writeNode(&eTree);
    }

    int Expression::getValue() const
    {
        return answer(&eTree);
    }
    
    void writeNode(const NodeType* node)
    {
        if(node->tag == SUB_NODE)
        {
            cout<<"(";
            writeNode(node->left);
            cout<< " "<<node->op<<" ";
            writeNode(node->right);
            cout<<")";
        }
        else if(node->tag == INT_ONLY)
            cout<<node->intValue;
    }
    int answer(const NodeType* node)
    {
        if(node->tag == INT_ONLY)
            return node->intValue;
        else
        {
            switch(node->op)
            {
            case '+':
                return answer(node->left) + answer(node->right);
            case '-':
                return answer(node->left) - answer(node->right);
            case '*':
                return answer(node->left) * answer(node->right);
            case '/':
                return answer(node->left) / answer(node->right);
            }
        }   
    }
}

Now I have the functions with const parameters and NodeType* node

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.