试图用c++编写我自己的链表实现,在点击列表中的3个元素后编写segfault代码
Trying to write my own linked list impementation in c++, code segfaults after hitting 3 elements in the list
我一直在尝试编写自己的链表实现,但当我尝试访问第三个元素或它之后的任何元素时,代码会出错。添加元素不会出错,但访问会出错。我在get()函数中找不到指针错误。
列表中的每个节点都存储(模板t的)数据和指向下一个节点的指针。我有两个函数,一个用于第一个元素,另一个用于任何后续元素。后续元素的get()函数总是segfault。我在函数中有一些调试消息,它们吐出了我无法解释的结果。例如,如果我对第二个元素,然后是第三个元素运行get()请求,代码不会segfault,但它确实返回了明显不正确的结果。我放置的调试消息表明,当第二个元素调用函数来检查第三个元素时,如果发生了segfault,就会发生segfault。尝试使用和不使用cout<lt;newList.get(2)<lt;endl;你会得到非常不同的结果。
一个可能的原因是指针存储-我让get()函数在循环通过时输出每个元素(第一个元素除外)的指针,并将它们与add()函数输出的指针进行比较,元素0和1的指针匹配,但2及以上的指针不匹配,我似乎不明白为什么会这样。
#include <iostream>
using namespace std;
template <class T> class myLinkedList{
T data;
myLinkedList<T> *next = NULL;
public:
myLinkedList(T input){
data = input;
}
void add(T input){
if(next == NULL){
myLinkedList<T> newItem(input);
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}else{
myLinkedList<T> nextEntry = *next;
nextEntry.add(input);
}
}
T getData(){
return data;
}
//the start of the get function, only used by the first entry in the list
T get(int entry){
int currentPosition = 0;
if(entry == currentPosition){
return getData();
}else{
//defrefrence the pointer anc check the next entry
myLinkedList<T> nextEntry = *next;
return nextEntry.get(entry, ++currentPosition);
}
}
private:
//this vesion is the hidden, private vesion only used by nodes other than the first one
//used to keep track of position in the list
T get(int entry, int currentPosition){
//cout << currentPosition << endl;
if(entry == currentPosition){
return data;
}else{
//derefrence the pointer and check the next entry
cout << next << endl;
myLinkedList<T> nextEntry = *next;
currentPosition++;
T output = nextEntry.get(entry, currentPosition);
return output;
}
}
};
int main(){
myLinkedList<int> newList(3);
newList.add(4);
newList.add(5);
newList.add(7);
newList.add(9);
cout << newList.get(2) << endl;
cout << newList.get(3) << endl;
return 0;
}
结果显然是错误的——程序应该吐出两组macthing指针,以及数字5和7(列表元素)
您的主要问题之一是:
if(next == NULL){
myLinkedList<T> newItem(input); // <<<<<<<<<<<<<
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}
在if
作用域内的堆栈上分配一个项。然后你把下一个指向这个项目。但是项的生存期受此范围限制。一旦您退出作用域,此项就不再存在。您需要通过"new"或其他方法动态分配它。
我有了突破!遵循Serge的解决方案是有帮助的,但还需要一个更改——而不是在我的add函数的else块中创建函数引用,例如
myLinkedList<T> nextEntry = *next;
nextEntry.add(input)
我需要直接使用指针,就像在中一样
next->add(input)
我不知道我的指针/对象语法
相关文章:
- 删除列表中的第n个元素
- 运行相同函数名称C++的多个类的列表
- 测试两个类型列表中的所有组合
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 如何在 c++ 中根据第二个元素按降序对列表进行排序
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 删除列表 c++ 中的最后 3 个元素
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- C++如果两个模板函数都与参数列表匹配,将调用哪个模板
- 如何通过保持第一个插入的位置来对列表中插入的其余元素进行排序?
- 如何在两个列表中比较和获取非包含值
- 如何在构造器的成员初始值设定项列表中调用两个函数?
- C++完全复制要字符串的字符列表,而不会忽略多个空格
- 如何获取C++中的输入并将其"split"到列表中?换句话说,取 N 个输入并放入 N 长度的数组中
- 只有我的"链接列表"中的第一个节点正在打印
- 如何在构造函数初始值设定项列表中使用 n 个元素初始化 std::vector<std::time_t>
- C++:随机数列表:x 个随机整数