段故障指针

时间:2012-03-11 22:29:01

标签: c++ memory pointers linked-list segmentation-fault

我试图实现一个链表数据类型的小样本。我创建了一个包含值的struct Node,一个跟踪所有节点的struct List(以链表格式)。最后一个类,它是两个结构的一般数据类型。

当我运行程序时,一旦到达该步骤,我总是遇到段错误错误: item-> link [i ++] = new Node(input)

代码:

struct Node
{
    int val;

    Node();
    Node(int x);
};

struct List
{
    Node *link[20];

    List();
};

class pol
{
    public:
        void read_txt(ifstream &file);

    private:
        List *item;
};

void pol::read_txt(ifstream &file)
{
    int input, i;
    i = 0;

    file >> input;

    cout << "Value read from the file: " << input << endl;

    while(input != 0)
    {
        item.link[i++] = new Node(input);
        file >> input;
        cout << "Value read from the file: " << input << endl;
    }
}

我要做的是,创建一个新的节点,其值为“input”,这是我从阅读文件中获得的。接下来,我想创建一个连接所有节点的链表。

1 个答案:

答案 0 :(得分:2)

成员item永远不会被初始化。据我所知,没有理由将其作为指针。只需将其List item;

相关问题