Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当我将一个节点指向一个数据字符时,为什么g++返回一个“分段错误”错误?_C++_Pointers_Linked List_Declaration - Fatal编程技术网

C++ 当我将一个节点指向一个数据字符时,为什么g++返回一个“分段错误”错误?

C++ 当我将一个节点指向一个数据字符时,为什么g++返回一个“分段错误”错误?,c++,pointers,linked-list,declaration,C++,Pointers,Linked List,Declaration,我有一个程序,在编译后返回一个错误,然后在g++中运行它。我知道,因为当我在VisualStudio中测试这段代码时,当我尝试将新节点数据指针设置为某个值时,会发生此错误。更具体地说,当我试图设置n->data=ch;VisualStudio在该行代码处中断。对于上下文,这里是我头文件的一部分,带有n->data=ch;最后: #include <ostream> class LinkedList { public: LinkedList(); ~L

我有一个程序,在编译后返回一个错误,然后在g++中运行它。我知道,因为当我在VisualStudio中测试这段代码时,当我尝试将新节点数据指针设置为某个值时,会发生此错误。更具体地说,当我试图设置n->data=ch;VisualStudio在该行代码处中断。对于上下文,这里是我头文件的一部分,带有n->data=ch;最后:

#include <ostream>

class LinkedList
{
public:
        LinkedList();
        ~LinkedList();

        void add(char ch);
private:
    struct node
    {
            node();
            char data;
            node * next;
    };
    node * head;
    node * curr;
    node * prev;
};
LinkedList::LinkedList() : head(nullptr), curr(nullptr), prev(nullptr);
LinkedList::node::node() : data('\0'), next(nullptr);
LinkedList::~LinkedList()
{
    if (!head) // head is null and so list is empty
    {
            return; //nothing to delete
    }

    for(curr = head; head; /* head isn't NULL*/ delete curr /*delete first element*/)
    {
            curr = head;  // set curr to head of list
            head = curr->next;  // move head over to next element (or make it null)
    }
}
void LinkedList::add(char ch)
{
    node * n = nullptr;
    n->next = nullptr; //my compiler doesn't like this
    n->data = ch; // or this
    //irrelevant code after this.
}
我希望我能给你们提供更多的背景,但我不知道为什么这不起作用。即使它确实与C字符串有关,我也不知道如何修复它。

作为您的代码

node * n = nullptr;
表示将空指针赋给n,然后通过

 n->next = nullptr;
因此,它会导致分割错误

解用

node * n = new node();
n->next = nullptr;
n->data = ch;
在标记为的第一行中,定义一个指向节点的指针,并将其初始化为null ptr,因此指针实际上不指向任何内容

因此,您不能使用该指针设置节点数据结构字段n->next和n->data的值,因为它与前一行一样不指向任何内容


要解决这个问题,您可以做的是创建一个新节点的实例,例如使用new,然后使用n->next和n->data准备该实例的字段。

编译器不会抛出错误。您可能希望替换node*n=nullptr;带节点*n=新节点;并将n存储在某个位置,在取消引用之前,必须使指针实际指向某个对象。请明确说明当我尝试将新节点数据指针设置为某个对象时,编译器抛出错误的确切含义。还有编译器怪胎。你真的是说编译失败了吗?或者你的意思是,它可以编译,但在运行结果时的行为与你预期的不一样?@Jon Skeet它可以编译,但行为与我预期的不一样。那么我需要在该函数末尾删除n,对吗?@LarryK:实际上,如果你实现了一个链表类,您可能希望将新创建的节点链接到列表的其余部分。删除列表节点的正确位置是在析构函数~LinkedList中。我的解构器现在足够了吗?如果我不想内存泄漏,我应该更改它吗?@LarryK:很抱歉,我现在没有足够的带宽来仔细检查你的析构函数代码。您可能希望自己调试它,以检查它是否正确地释放了每个节点。不过,乍一看,它似乎是正确的。谢谢,我只是想确保我的调试器没有任何重大错误。@NathanOliver,实际上他说了“//这之后是不相关的代码”,所以我们可以假设他可能会这样做。
void LinkedList::add(char ch)
{
    node * n = nullptr; // ##
    n->next = nullptr; //my compiler doesn't like this
    n->data = ch; // or this

    ...