我有一个头文件,我在其中定义方法。我最终试图实现一个双向链表来存储对数据。这是cont.h
template <class Key, class T> class storage;
template < class Key, class T, class Compare = std::less<Key> >
class cont{
private:
storage <Key, T> data;
public:
typedef size_t size_type;
typedef Key key_type;
typedef T mapped_type;
//default constructor
cont(){}
cont(key_type k, mapped_type m)
{
std::pair<Key, T> x = std::make_pair(k,m);
data.begin_list(x);
//std::pair<Key, T> a = std::make_pair(k,m);
//data.push(a);
}
};
template <class Key, class T>
struct node
{
node *prev, *next;
std::pair<Key, T> node_data;
};
template <class Key, class T>
class storage{
private:
node <Key, T> *head;
node <Key, T> *tail;
node <Key, T> *curr;
int size;
public:
//default ctor
storage(){}
void begin_list(std::pair <Key, T> h)
{
size=1;
//bottom two lines induce segmentation fault
//head->node_data=h;
//head->prev=NULL;
}
};
main.cc 将如下所示:
#include "cont.h"
int main() {
cont<double, int> s(6.6, 3);
}
我不明白为什么会出现段错误。我应该为每个节点动态分配内存吗?