0

わかりました、私はすでに後置記法でそれを持っており、次のような後置記法を持つ文字列変数を送信しています: 5 15 2 *+ これが私のコードです:

int evaluatePostFix(string postfix_expression){
//Create a new stack
stack<int> theStack;
//Loops while the postfix expression string still contains values
while(postfix_expression.length()>=1){
    //Loops on a number an whitespace
    while(isdigit(postfix_expression.at(0)) || isspace(postfix_expression.at(0))){
        //Holds a number that is above two digits to be added to the stack
        string completeNum;
        if(isdigit(postfix_expression.at(0))){ 
            //Add the digit so it can become a full number if needed
            completeNum+=postfix_expression.at(1);
        }
        else {
            //Holds the integer version of completeNum
            int intNum;
            //Make completeNum an int
            intNum=atoi(completeNum.c_str());
            //push the number onto the stack
            theStack.push(intNum);
        }
        //Check to see if it can be shortened
        if(postfix_expression.length()>=1){
            //Shorten the postfix expression
            postfix_expression=postfix_expression.substr(1);
        }
    }
    //An Operator has been found
    while(isOperator(postfix_expression.at(0))){
        int num1, num2;
        char op;
        //Grabs from the top of the stack
        num1=theStack.top();
        //Pops the value from the top of the stack - kinda stupid how it can return the value too
        theStack.pop();
        //Grabs the value from the top of the stack
        num2=theStack.top();
        //Pops the value from the top of the stack
        theStack.pop();
        //Grab the operation
        op=postfix_expression.at(0);
        //Shorten the postfix_expression
        postfix_expression=postfix_expression.substr(1);
        //Push result onto the stack
        theStack.push(Calculate(num1,num2, op));
    }
}
return theStack.top();

}

私が得るエラーは「Deque iterator not deferencable」です

このエラーについて私が得ることができる助けは大歓迎です。ところで、私はここ数年 C++ を使用していないので、少しさびています。

4

1 に答える 1

1

デバッガーでステップ実行して、エラーの原因となっている行を教えていただければ簡単です。ただし、エラーを見つけた可能性があると思います。

このコードブロックでは

if(isdigit(postfix_expression.at(0))){ 
    //Add the digit so it can become a full number if needed
    completeNum+=postfix_expression.at(1);
}

その要素が存在するかどうかを確認せずに、postfix_expression.at(1) を要求します。チェックがないため、不正なメモリ ロケーションにアクセスしている可能性があります。

于 2011-10-25T02:22:48.200 に答える