1

私の仕事は、スタックを表すリンクリストを使用して後置計算機を構築することです。電卓用に次のコードを記述しましたが、理解できないコンパイラエラーが発生します。エラーはpostfixcalc.cppファイルから発生しており、operand2 = opers.getTop(op)のifステートメントで発生しています。エラーには、「値は無視されるべきではない」と表示されます。私はDev-C++を使用していますが、このタイプのメッセージに遭遇したことはありません。任意の洞察をいただければ幸いです。また、必要に応じてStackPファイルを提供することもできます。

/** @file postfixcalc.h*/
#include "StackP.cpp"
#include <string>

using namespace std;
const int MAX_CHARS = 100;

class postfixcalc
{
public:
/** Default constructor. */
    postfixcalc();

// postfixcalc operations:
    void read();
    void show();
    int eval();

private:
    string e;
    Stack opers;
    char stringInput[MAX_CHARS];
    char *p;
};


/** @file postfixcalc.cpp*/

#include <iostream>
#include <cstring>
#include "postfixcalc.h"

using namespace std;

postfixcalc::postfixcalc()
{}

void postfixcalc::read()
{
    cout << "Please enter a postfix expression: ";
    getline(cin, e);

}//end read

void postfixcalc::show()
{
    cout << e << endl;
}//end show

int postfixcalc::eval()
{
    int operand1, operand2, result;
    int op;

    p = strtok(stringInput, " ");
    while(p)
    {
    op = p[0];
    if( op == '+' || op == '-' || op == '*' || op == '/')
    {
        operand2 = opers.getTop(op);
        opers.pop();
        operand1 = opers.getTop(op);
        opers.pop();

        switch(op)
        {
            case '+':
                    result = operand1 + operand2;
                    break;
            case '-':
                    result = operand1 - operand2;
                    break;
            case '*':
                    result = operand1 * operand2;
                    break;
            case '/':
                    result = operand1 / operand2;
                    break;
        }//end switch
        opers.push(result);
    }
    else
    {
        opers.push(op);
    }
    p = strtok(NULL, " ");
}//end while
}//end eval

StackPの実装は次のとおりです

/** @file StackP.h */

#include "StackException.h"
typedef int StackItemType;

/** ADT stack - Pointer-based implementation. */
class Stack
{
public:
// Constructors and destructor:

   /** Default constructor. */
   Stack();

   /** Copy constructor.
    * @param aStack The stack to copy. */
   Stack(const Stack& aStack);

   /** Destructor. */
   ~Stack();

// Stack operations:
   bool isEmpty() const;
   void push(const StackItemType& newItem) throw(StackException);
   void pop() throw(StackException);
   void pop(StackItemType& stackTop) throw(StackException);
   void getTop(StackItemType& stackTop) const
      throw(StackException);

private:
   /** A node on the stack. */
   struct StackNode
   {
      /** A data item on the stack. */
      StackItemType item;
      /** Pointer to next node.     */
      StackNode    *next;
   }; // end StackNode

   /** Pointer to first node in the stack. */
   StackNode *topPtr;
}; // end Stack

/** @file StackP.cpp */

#include <cstddef>   // for NULL
#include <new>       // for bad_alloc
#include "StackP.h"  // header file

using namespace std;

Stack::Stack() : topPtr(NULL)
{
}  // end default constructor

Stack::Stack(const Stack& aStack)
{
   if (aStack.topPtr == NULL)
      topPtr = NULL;  // original list is empty

   else
   {  // copy first node
      topPtr = new StackNode;
      topPtr->item = aStack.topPtr->item;

      // copy rest of list
      StackNode *newPtr = topPtr;    // new list pointer
      for (StackNode *origPtr = aStack.topPtr->next;
       origPtr != NULL; origPtr = origPtr->next)
      {  newPtr->next = new StackNode;
         newPtr = newPtr->next;
     newPtr->item = origPtr->item;
      }  // end for

      newPtr->next = NULL;
   }  // end if
}  // end copy constructor

Stack::~Stack()
{
   // pop until stack is empty
   while (!isEmpty())
      pop();
   // Assertion: topPtr == NULL
}  // end destructor

bool Stack::isEmpty() const
{
   return topPtr == NULL;
}  // end isEmpty

void Stack::push(const StackItemType& newItem)
        throw(StackException)
{
   // create a new node
   try
   {
      StackNode *newPtr = new StackNode;

      // set data portion  of new node
      newPtr->item = newItem;

      // insert the new node
      newPtr->next = topPtr;
      topPtr = newPtr;
   }
   catch (bad_alloc e)
   {
      throw StackException(
     "StackException: push cannot allocate memory.");
   }  // try
}  // end push

void Stack::pop() throw(StackException)
{
   if (isEmpty())
      throw StackException("StackException: stack empty on pop");
   else
   {  // stack is not empty; delete top
      StackNode *temp = topPtr;
      topPtr = topPtr->next;
      // return deleted node to system
      temp->next = NULL;  // safeguard
      delete temp;
   }  // end if
}  // end pop

void Stack::pop(StackItemType& stackTop) throw(StackException)
{
   if (isEmpty())
     throw StackException("StackException: stack empty on pop");
   else
   {  // stack is not empty; retrieve and delete top
      stackTop = topPtr->item;
      StackNode *temp = topPtr;
      topPtr = topPtr->next;

      // return deleted node to system
      temp->next = NULL;  // safeguard
      delete temp;
   }  // end if
}  // end pop

void Stack::getTop(StackItemType& stackTop) const throw(StackException)
{
   if (isEmpty())
      throw StackException("StackException: stack empty on getTop");
   else
      // stack is not empty; retrieve top
      stackTop = topPtr->item;
}  // end getTop

これはコンパイラエラーです:

C:\ Documents and Settings \ Owner \My...メンバー関数`intpostfixcalc :: eval()':

35 C:\ Documents and Settings \ Owner...void値は無視されるべきではないので無視されません

37 C:\ Documents and Settings \ Owner...void値は無視されるべきではないので無視されません

4

2 に答える 2

1

Stack::getTop()そのように宣言されています:

void getTop(StackItemType& stackTop);

void関数が値を返さないことを意味します。したがって、何も返さないため、呼び出しの結果をに割り当てようとすることopers.getTop()はできません。参照パラメーターを受け取るため、渡す変数に直接最上位の値を割り当てることができます。operand2getTop()getTop()

opers.getTop(operand2);

独自のクラスを作成する代わりにStack、次を使用できますstd::stack

#include <stack>
#include <iostream>

int main(int argc, char** argv) {
    std::stack<int> stack;
    stack.push(3);
    std::cout << stack.top() << '\n';
    stack.pop();
}

…もちろん、これが学習演習である場合を除いて、その場合は、将来のためにヒントを提出するだけです。何かがどのように行われるかを知ったら、他の人がすでに再利用可能な方法であなたのために仕事をしているという事実を利用する必要があります。

于 2012-03-27T17:15:32.810 に答える
1
void Stack::getTop(StackItemType& stackTop) const throw(StackException)

int postfixcalc::eval()
{
int operand1, operand2, result;
    operand2 = opers.getTop(op);

getTop関数は何も返さないので、それを何も受け取らずにに格納しますint。私はあなたがその行のためにこのような何かを望んでいたと思います:

   opers.getTop(operand2); 
于 2012-03-27T17:16:12.427 に答える