2012-03-27 7 views
1

내 임무는 스택을 나타 내기 위해 연결된리스트를 사용하여 포스트 픽스 계산기를 만드는 것입니다. 계산기에 대해 다음 코드를 작성했지만 필자가 이해하지 못하는 컴파일러 오류가 발생했습니다. postfixcalc.cpp 파일에서 오류가 발생하고 operand2 = opers.getTop (op)의 if 문에서 오류가 발생합니다. 오류는 "값을 무시해야합니다."라고 읽습니다. Dev-C++을 사용하고 있으며이 유형의 메시지가 발생하지 않았습니다. 모든 통찰력은 인정 될 것이다. 또한 필요한 경우 StackP 파일을 제공 할 수 있습니다.포스트 픽스 계산기 디버그

C : \ 문서 및 설정 \ 소유자 \ 내 ... 멤버 함수에서`INT의 postfixcalc 여기

/** @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 

이 컴파일러 오류가

/** @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 

StackP의 구현입니다 : : 평가() :

35 C : \ Documents 및 그것을 무시하지 설정 \ 소유자 ... 무효 값은한다고

37 C :

void getTop(StackItemType& stackTop); 

void

함수가 값을 반환하지 않는 것을 의미합니다 : \ 문서 및 설정 \ 소유자 ... 그것을 무시하지 무효 값은

+0

컴파일러로부터 완전한 에러 메시지를 붙여 넣을 수 있습니까? – Naveen

+0

StackP.cpp이란 무엇입니까? 왜 stl 스택을 사용하지 않습니까? 그것의 구현을 게시하시기 바랍니다. – WeaselFox

+0

Dev-C++는 오래된 것으로 더 이상 유지 관리되지 않으며 고대 컴파일러 버전 (GCC 3.4.5, 현재 4.7 일 때)과 함께 제공됩니다. 많은 개선 된 유사한 환경에서 [Code :: Blocks] (http://www.codeblocks.org/)를 시도해보십시오. –

답변

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); 
+0

도움을 주신 모든 분들께 감사드립니다. 나는 지금 문제를보고 그것을 알아 냈다고 생각한다. 당신들은 생명의 은인들입니다. – Eric

+0

@ user1277102 : 도움이되는 대답 (왼쪽)을 모두 upvote하고 _eventually_ 한 가지 대답을 최고로 표시하려면 체크 표시를 사용하십시오 (왼쪽에서 또한 하루 정도 기다려야 할 수도 있음) –

1

Stack::getTop()은과 같이 선언되어야한다 . 따라서 getTop()이 아무 것도 반환하지 않기 때문에 opers.getTop()operand2으로 호출하는 결과를 할당하려고 시도 할 수 없습니다! 당신이 통과 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(); 
} 

... 물론 이것이 학습 운동 인 경우가 아니라면 미래에 대한 정보를 간단히 정리하십시오. 어떻게하면 무언가가 완료되었는지 알게되면 다른 사람들이 이미 재사용 가능한 방식으로 작업을 수행 한 사실을 활용해야합니다.

+0

블라인드 촬영에 아주 좋습니다. –

+0

StackP의 구현을 게시 했으므로 사진을 볼 수 있습니다. 강사가 stl 스택 대신에 이것을 사용하기를 원합니다. – Eric

+0

@ user1277102 : 감사합니다. 이미 업데이트를보고 내 대답을 편집했습니다. –