繁体   English   中英

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

[英]Binary Expression Tree c++ Build Function

因此,我正在上一门计算机科学课,我们必须构建一个二进制表达式树,这是我到目前为止所拥有的:

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);

}

那是我建立一棵树的功能。 我的教授已将其写在板上,我们不得不对其进行一些编辑,但我在BTNP部分上一直遇到错误。

stack<BTNP> treestack;

我一直在到处寻找示例代码,以了解如何构建表达式树,但没有结果。 即使我没有得到回应,链接也会很好。

我的错误:

BTNP is not identified.

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

我得到的最后一个错误是关于treeStack.push(ptr);。

3 IntelliSense:没有重载函数实例“ std :: stack <_Ty,_Container> :: push [with _Ty = BET :: BTN,_Container = std :: deque>]”匹配参数列表参数类型为:(BET: :BTN *)对象类型为:std :: stack >>

错误2错误C2664:'void std :: stack <_Ty> :: push(BET :: BTN &&)':无法将参数1从'BET :: BTN *'转换为'BET :: BTN &&'

树的代码,或者我认为有些相关的代码。

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

假设此代码已复制并粘贴到问题中,则str.length之后缺少括号(尽管稍后已将其包括在注释中)。

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

将代码更改为str.length() ,它应该可以帮助您进一步:-)

暂无
暂无

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

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