【问题标题】:Binary Expression Tree c++ Build Function二进制表达式树 c++ 构建函数
【发布时间】:2015-09-17 19:23:31
【问题描述】:

所以我正在上一门计算机科学课,我们必须构建一个二叉表达式树,这就是我目前所拥有的:

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 &lt; 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;
};

【问题讨论】:

  • 忘记添加 BTN 是我的节点功能。
  • 你遇到了什么错误?
  • 编辑您的原始问题并在其中添加所有内容。它不属于 cmets
  • 我已经编辑了我原来的问题。我很抱歉。

标签: c++ tree expression binary-tree


【解决方案1】:
// ...snip
int i = 0;
while(i < str.length)
{
    // etc...

假设此代码被复制并粘贴到问题中,您在str.length 之后缺少括号(尽管您稍后将它们包含在评论中)。

所以函数实际上并没有被调用,编译器抱怨(非常正确)它不知道如何告诉你成员函数是否大于int

将代码更改为str.length(),它应该可以帮助您走得更远:-)

【讨论】:

  • 这对我帮助很大,我现在正在捂脸。大声笑但是我的代码中仍然是“eh”的部分是 treeStack.push(ptr);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多