2012-10-27 2 views
0

그래서 스택 ADT의 링크 된 목록 기반 구현을 최근에 작성했습니다. 그러나 스택의 노드가 선언되는 방식과 약간의 불일치가있는 이유는 확실하지 않습니다. 컴파일러는 매우 화가 나서 특정 기능을 위해 특정 방법을 쓸 때까지 컴파일되지 않습니다. 나는 이것이 왜 그런지에 대해 매우 궁금합니다.Visual Studio에서 StackNode를 두 가지 방식으로 강제 실행하는 이유는 무엇입니까?

다음은 컴파일러에서 서로 다른 두 가지 형식을 필요로하는 두 가지 다른 방법입니다.

여기 컴파일러가 원하는 내 소멸자가 StackNode *temp입니다.

template <typename DataType> 
StackLinked<DataType>::~StackLinked() { 
    StackNode *temp; 
    while (top != 0) { 
     temp = top; 
     top = top->next; 
     delete temp; 
    } 
} 

여기에 컴파일러가 StackNode<DataType> *temp을 원하는 내 할당 연산자 오버로드입니다.

template <typename DataType> 
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other) { 
    if (this != &other) { 
     StackNode<DataType> *newNode, *current, *last; 

     if (top != 0) { 
      StackNode<DataType> *temp; 
      while (top != 0) { 
       temp = top; 
       top -> top->next; 
       delete temp; 
      } 
     } 

     if (other.top == 0) { 
      top = 0; 
     } 
     else { 
      current = other.top; 
      top = new StackNode<DataType>; 
      top->dataItem = current->dataItem; 
      top->next = 0; 
      last = top; 
      current = current->next; 

      while (current != 0) { 
       newNode = new StackNode<DataType>; 
       newNode->dataItem = current->dataItem; 
       newNode->next = 0; 
       last-> next = newNode; 
       last = newNode; 
       current = current->next; 
      } 
     } 
    } 
    return *this; 
} 

이 왜 몰라,하지만 알 수없는 날 귀찮게한다.

참고 : 내 StackNode 클래스는 StackLinked 클래스의 내부 클래스입니다.

편집 : 클래스 선언 : 다른 세부 사항이 필요한 경우

#ifndef STACKARRAY_H 
#define STACKARRAY_H 

#include <stdexcept> 
#include <iostream> 

using namespace std; 

#include "Stack.h" 

template <typename DataType> 
class StackLinked : public Stack<DataType> { 

public: 

StackLinked(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); 
StackLinked(const StackLinked& other); 
StackLinked& operator=(const StackLinked& other); 
~StackLinked(); 

void push(const DataType& newDataItem) throw (logic_error); 
DataType pop() throw (logic_error); 

void clear(); 

bool isEmpty() const; 
bool isFull() const; 

void showStructure() const; 

private: 
class StackNode { 
    public: 
StackNode(const DataType& nodeData, StackNode* nextPtr); 
DataType dataItem; 
StackNode* next; 
}; 

StackNode* top; 
}; 

#endif 

. 그냥 물어봐! 시간 내 주셔서 감사합니다!

+0

클래스 선언을 볼 수 있습니까? 할당 담당자가 올바르게 선언 되었습니까? –

+0

내 기본 테스트 g ++ 4.7.2는'StackNode * temp;와 함께 만족스러워 보인다. – Neil

+0

요청 당 클래스 선언으로 내 질문을 업데이트했다. –

답변

0

은 클래스 템플릿이 아니기 때문에 표시된 코드에서 StackNode<DataType>이 올바르지 않습니다.

이렇게하면 컴파일러가 찾는 StackNode이라는 이름의 템플릿이 있다고 생각됩니다. 어떤 파일에 StackNode의 다른 버전이 포함되어 있는지 확인하십시오.

+0

재미있는 점은 StackNode 위에 커서를 올리면 나타나는 것입니다. Visual Studio에서 StackLinked에서 온 것입니다. :: StackNode. 그러나 컴파일러는 여전히 그것을 받아들입니다. 이것은 제가 만든 StackNode의 유일하고 유일한 버전입니다. –

관련 문제