简体   繁体   English

二进制表达式树c ++构建函数

[英]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. 我的教授已将其写在板上,我们不得不对其进行一些编辑,但我在BTNP部分上一直遇到错误。

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: while(i < str.length())得到3个不同的错误,例如:

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); 我得到的最后一个错误是关于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>> 3 IntelliSense:没有重载函数实例“ std :: stack <_Ty,_Container> :: push [with _Ty = BET :: BTN,_Container = std :: deque>]”匹配参数列表参数类型为:(BET: :BTN *)对象类型为:std :: stack >>

Error 2 error C2664: 'void std::stack<_Ty>::push(BET::BTN &&)' : cannot convert parameter 1 from 'BET::BTN *' to 'BET::BTN &&' 错误2错误C2664:'void std :: stack <_Ty> :: push(BET :: BTN &&)':无法将参数1从'BET :: BTN *'转换为'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). 假设此代码已复制并粘贴到问题中,则str.length之后缺少括号(尽管稍后已将其包括在注释中)。

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 . 因此实际上并未调用该函数,并且编译器在抱怨(很正确)它不知道如何告诉您成员函数是否大于int

Change the code to str.length() and it should help you get further :-) 将代码更改为str.length() ,它应该可以帮助您进一步:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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