简体   繁体   中英

Binary Expression Tree c++ Build Function

So I'm taking a computer science class, and we have to build a binary expression tree and here is what I have so far:

void buildtree(string str)
{
    string tmp;
    stack<char> opstack;
    stack<BTNP> treeStack;
    double value;
    int i = 0;
    while(i < str.length)
    {
        while(isspace(str[i++]))
        {
            if(str[i] == '(')
            {
                opstack.push(str[i]);
            }
            else if(isdigit(str[i] || str[i] == '*'))
            {
                tmp.clear();
                while(isdigit(str[i]) || str[i] == '+')
                {
                    tmp =+ str[i++];
                }
            }
        }
    }
    BTNP ptr= new BTN;
    ptr->flag = false;
    ptr->value = value;
    ptr->left = NULL;
    ptr->right = NULL;
    treeStack.push(ptr);

}

That is my function to build a tree. My professor had written it on the board and we had to edit it some bit but I keep getting an error with the BTNP part.

stack<BTNP> treestack;

I've been searching everywhere for a sample code to see how to build an expression tree but nothing's come up. Even if I do not get a response a link would be nice.

My errors:

BTNP is not identified.

while(i < str.length()) gets 3 different errors such as:

Error   3   error C2297: '<' : illegal, right operand has type 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Alloc>::* )(void) throw() const'  

Error   2   error C2446: '<' : no conversion from 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Alloc>::* )(void) throw() const' to 'int'  

Error   1   error C3867: 'std::basic_string<_Elem,_Traits,_Alloc>::length': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Alloc>::length' to create a pointer to member   

    4   IntelliSense: no instance of overloaded function "std::stack<_Ty, _Container>::push [with _Ty=BET::BTN, _Container=std::deque<BET::BTN, std::allocator<BET::BTN>>]" matches the argument list
            argument types are: (BET::BTN *)
            object type is: std::stack<BET::BTN, std::deque<BET::BTN, s

The final error I am getting is with the treeStack.push(ptr);

3 IntelliSense: no instance of overloaded function "std::stack<_Ty, _Container>::push [with _Ty=BET::BTN, _Container=std::deque>]" matches the argument list argument types are: (BET::BTN *) object type is: std::stack>>

Error 2 error C2664: 'void std::stack<_Ty>::push(BET::BTN &&)' : cannot convert parameter 1 from 'BET::BTN *' to 'BET::BTN &&'

the Code for the tree, or what I assume is somewhat related..

struct BTN
{
    bool flag;
    char op;
    double value;
    BTN * left;
    BTN * right;
    BTN * BTNP;
};
// ...snip
int i = 0;
while(i < str.length)
{
    // etc...

Assuming this code is copy-and-pasted into the question, you're missing the brackets after str.length (though you've included them in the comment later on).

So the function isn't actually being called, and the compiler is complaining (quite rightly) that it doesn't know how to tell you whether a member function is greater than an int .

Change the code to str.length() and it should help you get further :-)

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