代码行正在杀死我的程序
code line is killing my program
我在c++中制作了这个链表类,它工作得很好,除了在我运行它之后程序没有响应。我已经找到了导致问题的线路,但我不知道为什么。即使我用不同的方式输入,它也会做同样的事情。
这是我的list类:
#include <string>
template<class T>
class List : public Object{
private:
Node<T>* first;
Node<T>* last;
int length;
public:
List() : Object(new std::string("List")) {
first = NULL;
last = NULL;
length = 0;
}
~List() {
delete first;
delete last;
}
void Add(T value) {
if(first==NULL)
first = new Node<T>(NULL, value);
else if(last==NULL)
---->last = new Node<T>(first, value);<-----
else
last = new Node<T>(last, value);
length++;
}
T Remove(T value) {
Node<T>* temp = first;
while(temp!=NULL) {
if(temp->GetValue()==value) {
temp->GetPrev()->SetNext(temp->GetNext());
temp->GetNext()->SetPrev(temp->GetPrev());
delete temp;
length--;
return value;
}
temp = temp->GetNext();
}
return 0;
}
T Get(int index) {
Node<T>* temp = first;
int i = 0;
while(temp!=NULL) {
if(i==index)
return temp->GetValue();
i++;
temp = temp->GetNext();
}
return 0;
}
};
当我删除上面的标记线时,程序没有响应。这是我的Node构造函数:
#include <string>
template<class T>
class Node : public Object{
private:
Node* next;
Node* prev;
T value;
public:
Node(Node* prev, T value) : Object(new std::string("Node")){
if(prev!=NULL) {
prev->next = this;
this->prev = next;
}
next = NULL;
this->value = value;
}
~Node() {
delete next;
}
T GetValue() {
return value;
}
Node* GetNext() {
return next;
}
Node* GetPrev() {
return next;
}
};
my object class:
#include <string>
class Object {
private:
std::string* type;
public:
Object() {
type = new std::string("Object");
}
Object(std::string* type) {
this->type = type;
}
~Object() {
delete type;
}
std::string* GetType() {
return type;
}
};
我Test.cpp #include <iostream>
#include <string>
#include "Object.h"
#include "Node.h"
#include "List.h"
using namespace std;
int main () {
List<int> l;
l.Add(5);
l.Add(93);
l.Add(17);
l.Add(7789);
l.Add(60);
cout << "node 4 is:" << l.Get(3) << endl;
return 0;
}
错误图像http://i50.tinypic.com/2mw5phi.png感谢您的阅读,请尽快提供帮助,如果您需要我提供更多信息,请评论。
Edit:你的程序有很多问题,但是什么可能导致你的崩溃:你的Add
-函数不能正常工作。应该是这样的:
if(first==NULL) {
first = new Node<T>(NULL, value);
last = first;
} else {
last = new Node<T>(last, value);
}
length++;
否则,它将不能正确插入第二个元素。为什么?使用原始代码,在第一次添加之后,由于else
,您的last
仍然为NULL。因此,在第二次添加时,您将最后设置为new Node<T>(NULL, value)
。因此,它不会赋值第一个元素的next
指针。而且你的清单会不一致。
除此之外,还有双重自由,不必要的堆分配string
字段在你的对象类,所有权问题等。再给您举一个例子:您的List
析构函数将由于双重free而导致堆损坏。调用delete first
将删除Node
析构函数中由于delete next
而产生的所有节点,只要列表是一致的。然后调用delete last
,但是该对象已经被释放了。这将破坏您的程序的内存管理,也可能导致程序退出时崩溃。
这个函数对你来说是否正确??
上面写的是GetPrev
,实际上是next
。
Node* GetPrev() {
return next;
}
我发现,如果我在Node
构造函数中注释掉这一行,代码将编译:
if (next != NULL) {
// next->next = this;
prev = next;
}
编辑1:
我也意识到你在你的Node
课上这样做:
private:
Node* next;
Node* prev;
T value;
由于这些对象是在Node
类中声明的,因此它们此时是不完整类型。我设法把这个问题简化成这样一个简单的问题:
template <class T>
struct S {
S* s = new S();
~S() { delete s; }
};
int main() {
S<int> s; // Segmentation fault (core dumped) ./test > .stdout
}
这会导致崩溃,因为S
本身是一个不完整类型。
我得到相同的分割错误,因为我得到了你的代码。我很确定这是因为Node
类中的指针是建立在不完整类型上的;并且从它们访问数据是在寻找不属于你的内存,因此崩溃。
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- 为什么我的 c++ 程序检查不是初始化的变量?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- C++ - 为什么我的程序在再次调用函数后关闭?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 我需要如何更改我的程序以使用打开/关闭原则?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较