gpt4 book ai didi

设置对象属性时出现 C++ 内存访问冲突

转载 作者:行者123 更新时间:2023-11-27 22:29:43 25 4
gpt4 key购买 nike

无法使我的一部分代码正常工作。建立一个基本的链表来学习指针。我想我已经记下了大部分内容,但是任何尝试使用我创建的函数 (push_back) 都会在为指针设置值时引发内存访问错误。

不太确定哪里出了问题,因为使用 push_front 效果很好,几乎完全一样。

有什么想法吗? =/

代码:

驱动.cpp

#include <string>
#include <iostream>
#include "linklist.h"
#include "node.h"

using namespace std;

// printList function
// Purpose: Prints each node in a list
// Returns: None.
// Pre-Conditions: List must have member nodes.
// Post-Conditions: None.
void printList(linklist);

int main()
{
linklist grocery;

grocery.push_front(new node("milk", "1 gallon"));
grocery.push_front(new node("bread","2 loaves"));
grocery.push_front(new node("eggs","1 dozen"));
grocery.push_front(new node("bacon","1 package"));
cout << "First iteration:" << endl;
printList(grocery);
cout << "----------------------" << endl << endl;

grocery.push_front(new node("hamburger","2 pounds"));
grocery.push_front(new node("hamburger buns", "1 dozen"));
cout << "Second iteration:" << endl;
printList(grocery);
cout << "----------------------" << endl << endl;

node* deleteMe = grocery.pop_front();
delete deleteMe;
cout << "Third iteration:" << endl;
printList(grocery);
cout << "----------------------" << endl << endl;

grocery.push_back(new node("orange juice","2 cans"));
grocery.push_back(new node("swiss cheeese","1 pound"));
cout << "Fourth iteration:" << endl;
printList(grocery);
cout << "----------------------" << endl << endl;

deleteMe = grocery.pop_back();
delete deleteMe;
cout << "Fifth iteration:" << endl;
printList(grocery);
cout << "----------------------" << endl << endl;

while (grocery.getNodeCount() != 0)
{
deleteMe = grocery.pop_front();
cout << "Cleaning: " << deleteMe->getDescription() << endl;
delete deleteMe;
}

system("PAUSE");
return 0;
}

void printList(linklist input)
{
node* temp = input.getFirst();
for (int i = 0; i < (input.getNodeCount()); i++)
{
cout << temp->getQuantity() << " " << temp->getDescription() << endl;

temp = temp->getNextNode();
}
}

节点.h

#pragma once
#include <string>

using namespace std;

class node
{
public:
// Default Constructor
// Values, "none", "none", NULL.
node();

// Parameterized Constructor
// nextNode initialized NULL and must be explicitly set.
node(string descriptionInput, string quantityInput);

// getDescription function
// Purpose: Returns node description.
// Returns: string
// Pre-Conditions: None.
// Post-Conditions: None.
string getDescription();

// setDescription function
// Purpose: Sets node description
// Returns: Void
// Pre-Conditions: None
// Post-Conditions: None
void setDescription(string);

// getQuantity function
// Purpose: Returns node quantity.
// Returns: string
// Pre-Conditions: None.
// Post-Conditions: None.
string getQuantity();

// setQuantity function
// Purpose: Sets node quantity
// Returns: Void
// Pre-Conditions: None
// Post-Conditions: None
void setQuantity(string);

// getNextNode function
// Purpose: Returns pointer to next node in list sequence.
// Returns: node pointer
// Pre-Conditions: None.
// Post-Conditions: None.
// Note: Not set during initialization. Must be explicitly done.
node* getNextNode();

// setNextNode function
// Purpose: Sets pointer to next node in list sequence.
// Returns: None.
// Pre-Conditions: None.
// Post-Conditions: None.
// Note: Not set during initialization. Must be explicitly done.
void setNextNode(node*);
private:
string description;
string quantity;
node* nextNode;
};

节点.cpp

#include "node.h"


node::node()
:description("none"),
quantity("none"),
nextNode(NULL)
{}

node::node(string descriptionInput, string quantityInput)
:description(descriptionInput),
quantity(quantityInput),
nextNode(NULL)
{}

string node::getDescription()
{
return description;
}

void node::setDescription(string descriptionInput)
{
description = descriptionInput;
}

string node::getQuantity()
{
return quantity;
}

void node::setQuantity(string quantityInput)
{
quantity = quantityInput;
}

node* node::getNextNode()
{
return nextNode;
}

void node::setNextNode(node* input)
{
nextNode = input;
}

链表.h

#pragma once
#include "node.h"

class linklist
{
public:
// Constructor
// Builds an empty list
linklist();

// push_front function
// Purpose: Takes node pointer. Places that node at beginning of list.
// Returns: None
// Pre-Conditions: None
// Post-Conditions: None
void push_front(node*);

// pop_front function
// Purpose: Removes first node from list.
// Returns: Node pointer. NODE IS NOT DESTROYED.
// Pre-Conditions: List must have a node to remove.
// Post-Conditions: Node is not destroyed.
node* pop_front();

// getFirst function
// Purpose: Returns node pointer to first node in list
// Returns: node pointer
// Pre-Conditions: List must have a node added.
// Post-Conditions: None.
node* getFirst();

// push_back function
// Purpose: Takes node pointer. Places that node at end of list.
// Returns: None
// Pre-Conditions: None
// Post-Conditions: None
void push_back(node*);

// pop_back function
// Purpose: Removes last node from list.
// Returns: Node pointer. NODE IS NOT DESTROYED.
// Pre-Conditions: List must have a node to remove.
// Post-Conditions: Node is not destroyed.
node* pop_back();

// getNodeCount function
// Purpose: Returns nodeCount
// Returns: int
// Pre-Conditions: None.
// Post-Conditions: None.
int getNodeCount();
private:
node* firstNode;
node* lastNode;
int nodeCount;
};

链表.cpp

#include "linklist.h"


linklist::linklist()
:firstNode(NULL),
lastNode(NULL),
nodeCount(0)
{}

void linklist::push_front(node* input)
{
node* temp = getFirst();

input->setNextNode(temp);

firstNode = input;
nodeCount++;
}

node* linklist::pop_front()
{
node* temp = getFirst();

firstNode = temp->getNextNode();

nodeCount--;
return temp;
}

node* linklist::getFirst()
{
return firstNode;
}

void linklist::push_back(node* input)
{
node* temp = lastNode;

temp->setNextNode(input);

lastNode = temp;
nodeCount++;
}

node* linklist::pop_back()
{
node* oldLast = lastNode;
node* temp = firstNode;

// find second to last node, remove it's pointer
for (int i = 0; i < (nodeCount - 1); i++)
{
temp = temp->getNextNode();
}
temp->setNextNode(NULL);

lastNode = temp;

nodeCount--;
return oldLast;
}

int linklist::getNodeCount()
{
return nodeCount;
}

最佳答案

你在推送方法中有错误,因为当你在前面推送时你不控制这个元素是否也是最后一个,当你在最后推送时类似。这会导致您没有连接整个列表,因为开始部分不知道结束部分。我希望这是可以理解的。

pop 方法也是错误的 - 同样的问题。您无法控制列表是否为空

关于设置对象属性时出现 C++ 内存访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4281146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com