0

リンクリストクラスをlist内部で使用する Stack クラスがあります。メイン関数では、push および pop メンバー関数が指定されたスタックを正常に変更します。スタックを取り、それに対していくつかの操作を実行する別の関数を作成しました。内部的には、プッシュとポップを使用します。奇妙なことに、この関数内で変更されているように見えますが、実行後、スタックは変更されません。コードの一部を提供します (必要に応じてさらに追加できます)。

void run_stack_op(Stack stack, string token) {
    int operand1 = stack.pop();
    int operand2 = stack.pop();
    intfunc f = fmap[token];
    cout << operand1 << ", " << operand2 << ", " << f(operand2, operand1) << endl;
    stack.push(f(operand2, operand1));
    cout << "current stack (in run_stack_op): " << stack.to_string() << endl;
}

...それから主に:

s = Stack();
s.push(3); s.push(4);
run_stack_op(s, "-");
cout << "current stack (in main): " << s.to_string() << endl;
int val = s.pop();
cout << "should be -1: " << val << endl;

その結果:

4, 3, -1
current stack (in run_stack_op): -1 
current stack (in main): 4 3 
should be -1: 4
4

1 に答える 1