쇼핑 목록을 저장할 단일 링크 된 목록이 포함 된 프로그램을 작성하고 있습니다. 각 노드에는 항목 이름, 수량 및 수량 설명 (예 : 달걀의 경우 12 개)이 있습니다. 모든 것은 소멸자를 제외하고 프로그램에서 찾아냅니다. 나는 그것이 무엇이 잘못되었는지 찾아내는 것처럼 보이지 않는다. 프로젝트 14 0x0FC7A9E8
(msvcr120d.dll
)에Big Three를 올바르게 구현하는 방법 : 단독 링크 된 목록 (C++)
"처리되지 않은 예외 : 코드 다음 소멸자 호출
return 0;
되고 메시지와 광고delete current;
멈 여기서드라이버 끝까지 실행한다. exe :
0xC0000005
: 액세스 위반 읽기 위치0xFEEEFEE2
. ".
아래의 세 가지 기능에 대한 구현을 게시했습니다. 기본 생성자는 두 포인터 (first
, last
)를 null
으로, nodeCount
을 0
으로 초기화합니다.
문제를 찾을 수 없습니다. 어떤 도움이 필요합니까?
List::List(const List& b)
{
Node* newNodePtr = new Node;
Node* nodeCopy = b.first;
newNodePtr = nodeCopy;
first = newNodePtr;
last = newNodePtr;
nodeCount++;
nodeCopy = nodeCopy->getNext();
while (last != b.last)
{
Node* newNode = new Node;
newNode = nodeCopy;
Node* currentNode = last;
currentNode->setNext(newNode);
last = newNode;
nodeCount++;
nodeCopy = nodeCopy->getNext();
}
}
List::~List()
{
Node* current = first;
while (current != nullptr)
{
Node* _next = current->getNext();
delete current;
current = _next;
}
first = nullptr;
last = nullptr;
}
List& List::operator=(const List& rho)
{
Node* current = first;
while (current != nullptr)
{
Node* _next = current->getNext();
delete current;
current = _next;
}
first = nullptr;
last = nullptr;
Node* newNodePtr = new Node;
Node* nodeCopy = rho.first;
newNodePtr = nodeCopy;
first = newNodePtr;
last = newNodePtr;
nodeCount++;
nodeCopy = nodeCopy->getNext();
while (last != rho.last)
{
Node* newNode = new Node;
newNode = nodeCopy;
Node* currentNode = last;
currentNode->setNext(newNode);
last = newNode;
nodeCount++;
nodeCopy = nodeCopy->getNext();
}
return *this;
}
편집 : 기록 된대로 나는 또한 내와 push_back 기능을 추가했습니다 :
void List::push_back(Node* newNode)
{
if (first == nullptr)
{
first = newNode;
last = newNode;
}
else
{
Node* currentNode = last;
currentNode->setNext(newNode);
last = newNode;
}
nodeCount++;
}
좋아 내가 아웃 냈어요 생각합니다. 이 코드는 작동하는 것 같고 내 교수가 제공하는 드라이버에 맞습니다.
이List::List(const List& b)
{
this->copyList(b);
}
List::~List()
{
this->clearList();
}
List& List::operator=(const List& rho)
{
this->clearList();
this->copyList(rho);
return *this;
}
void List::clearList()
{
Node* current = first;
while (current != nullptr)
{
current = pop_front();
delete current;
current = first;
}
first = nullptr;
last = nullptr;
}
void List::copyList(const List& b)
{
first = nullptr;
last = nullptr;
nodeCount = 0;
Node *headNode = b.getFirst();
while (headNode != nullptr)
{
string des = headNode->getDescription();
string qNa = headNode->getQuantityName();
int qNu = headNode->getQuantityNumber();
Node* newNode = new Node(qNu, qNa, des);
push_back(newNode);
headNode = headNode->getNext();
}
}
Node* List::pop_front()
{
Node* saveFirst = first;
first = first->getNext();
nodeCount--;
return saveFirst;
}
void List::push_back(Node* newNode)
{
if (nodeCount == 0)
{
first = newNode;
last = newNode;
}
else
{
Node* currentNode = last;
currentNode->setNext(newNode);
last = newNode;
}
nodeCount++;
}
'노드'의 소멸자는 어떻습니까? 그걸 보여 주시겠습니까? – 0x499602D2
저는 교수님이 Node 클래스의 소멸자를 포함하지 말라고 들었습니다. – JamaicanBambi
이 코드는 안전하지 못하고 믿을 수 없을만큼 예외적이며 끔찍하게 누출됩니다. 교수님이'std :: shared_ptr'도 사용하는 것을 금하셨습니까? – Mgetz