分段 实例化时出错
Segmentation Faults upon instantiation
我目前正在使用链接列表数据结构。该列表具有指向头节点的指针和指向尾节点的指针。
节点类如 follode
template<class Type> class Node
{
private:
Type* storedVertex;
Node<Type>* next;
public:
Node()
{
storedVertex = NULL;
next = NULL;
}
Node(Type &vertex): next(NULL)
{
storedVertex = &vertex;
}
Type* get_Vertex()
{
return storedVertex;
}
Node<Type>* get_Next()
{
return this->next;
}
void set_Next(Node<Type>* _node)
{
this->next = _node;
}
};
类:
template<class Type> class LinkList
{
private:
int sz; //number of entries in linked list.
Node<Type> *head = NULL;
Node<Type> *tail = NULL;
public:
LinkList(): sz(0)
{
//O(1)
}
~LinkList()
{}
void del_All()
{
while (head != NULL)
{
Node<Type>* tmp = head;
head = head->next;
delete tmp;
}
sz = 0;
tail = NULL;
}
// /*
void push_Front(Type _vertex)
{
Node<Type>* _node = new Node<Type>(_vertex);
if(head == NULL)
{
head = _node;
tail = _node;
sz++;
}
else
{
_node->next = head;
head = _node;
sz++;
}
}
void add_node(Type _vertex)
{
Node<Type>* _node = new Node<Type>(_vertex);
//Adds node to the back of the linkList.
if (head == NULL)
{
//cout << "ptr" << ptr->getNext() << endl;
head = _node;
tail = _node;
sz++;
}
else
{
Node<Type>* ptr = head;
while(ptr->get_Next() != NULL)
{
ptr = ptr->get_Next();
}
ptr->set_Next(_node);
this->tail = _node;
sz++;
}
//LinkList::printList();
}
bool is_Empty()
{
if(head==0) cout << "Empty" << endl;
return head==0;
}
Node<Type>* get_Head()
{
cout << "DEBUG:: IN GET HEAD" << endl;
if(head == NULL) return NULL;
return head;
}
void set_Head(Node<Type> _Node)
{
push_Front(_Node);
}
Type get_Tail()
{
if(tail == NULL) return NULL;
return tail;
}
//Accessors
int size()
{
return sz;
}
bool search_for(string data)
{
cout << "DEBUG 3" << endl;
if(this->get_Head() == NULL)
{
cout << "DEBUG 3.5" << endl;
return false;
}
else
{
cout << "DEBUG 4" << endl;
Node<Type>* ptr = head;
if(ptr = NULL) return false;
cout << "DEBUG AGAIN" << endl;
cout << "search_for() while loop was ";
while(ptr != NULL)
{
if(ptr->get_Vertex()->get_Data() == data)
{
cout << "not skipped" << endl;
return true;
}
ptr = ptr->get_Next();
cout << "not ";
}
cout << "skipped.";
return false;
}
}
};
主要:
#include <iostream>
#include <string>
#include "Vertex.h"
#include "HashTable.h"
using namespace std;
int main()
{
LinkList<string> list;
cout << "*****Insert*****" << endl;
cout << "Data(string) to insert: ";
string data;
cin >> data;
Vertex<string>* vertex = new Vertex<string>(data);
bool insert;
insert = list.add_node(vertex);
return 0;
}
这些文件正在我这边编译。当我运行 add_Node 函数时,它会一直运行,直到遇到任何语句,例如 (head == NULL)。这就是我遇到赛格错误的地方。我不明白这是因为我发起了 NULL 头吗?
我能看到的唯一错误是:
if(ptr = NULL) return false;
相关文章:
- 从C++实例化QML
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 约束和显式模板实例化
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 对象实例化调用构造函数的次数太多
- 如何使用非默认构造函数实例化模板化类
- 静态数据成员模板专用化的实例化点在哪里
- 从模板类初始化对象时出错.参考类模板实例化
- 重载前模板实例化出错
- 实例化没有继承的对象时出错
- 使用bool非类型参数实例化类模板时出错
- 使用 std::array::size 实例化 std::array 时出错
- Arduino C++ - 尝试实例化具有非类型模板参数的类时出错
- 实例化basic_regex类型数组时出错
- 实例化函数模板时出错
- 在XCode/Clang中使用std::shared_ptr实例化std::map出错
- 分段 实例化时出错
- 转换基指针时出错:“不能实例化抽象类”