그래서 스택 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
. 그냥 물어봐! 시간 내 주셔서 감사합니다!
클래스 선언을 볼 수 있습니까? 할당 담당자가 올바르게 선언 되었습니까? –
내 기본 테스트 g ++ 4.7.2는'StackNode * temp;와 함께 만족스러워 보인다. – Neil
요청 당 클래스 선언으로 내 질문을 업데이트했다. –