簡體   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