c++ - 我收到错误 "invalid null pointer"

标签 c++ debugging pointers null assertion

我想从文件中读取学生姓名并将其插入到我的链表中,但我遇到了错误框的问题。错误显示为“表达式:无效的空指针。”

我用谷歌搜索却没有这样的运气。我想我知道哪里出错了,但我不知道如何解决。

如果你能帮忙,那就太好了!

这是我的代码:

P.S 我还没有完成,所以我的代码可能不完整,我现在只是想清除所有错误,这样我最后就不会再犯三倍的错误了。

LList.h

#include <iostream>
#include <iomanip>
#include <string>

#ifndef LLIST_H
#define LLIST_H

typedef int ElementType;

class LList
{
public:
    LList();
    ~LList();
    void insert(std::string new_data);
    void display();
    void remove(std::string delete_data);

private:
    class Node
    {
    public:
        std::string data;
        Node *next;

        Node(std::string data_value = NULL);
    };

    Node *head;
    int mySize;
};

#endif LLIST_H

LList.cpp

#include <iomanip>
#include <iostream>
#include <string>
#include "LList.h"

using namespace std;

LList::Node::Node (string data_value)
{
    this -> data = data_value;
    this -> next = NULL;
}

LList::LList()
{
    this -> head = new Node(0);
    mySize = 0;
    string data = "";
}

LList::~LList()
{
    delete this -> head;
}

void LList::insert(string new_data)
{
    Node *tempHolder;
    tempHolder = this->head;

    while (tempHolder->next != NULL)
    tempHolder = tempHolder -> next;

    Node *newNode = new Node(new_data);
    tempHolder ->next = newNode;
    this->mySize++;
}

void LList::display()
{
    Node *temp;
    temp = head->next;

    while(temp -> next != NULL)
    {
        cout << temp -> data << endl;
        temp = temp -> next ;   
    }
}

void LList::remove(string delete_data)
{
    Node *tempHolder;
tempHolder = head;
while (tempHolder->next != NULL ) 
{
    if (tempHolder->next->data == delete_data)
   {
      Node *delete_ptr = tempHolder->next;
      tempHolder->next = tempHolder->next->next;
      delete delete_ptr;
      mySize-- ;
      break;
   } else
     tempHolder = tempHolder->next;
}
}

main.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include "LList.h"
#include <fstream>

using namespace std;

int main()
{
    LList student;

    ifstream infile;
    char readLine[500];

    infile.open ("names.txt");

    if(infile.is_open())
    {
        while (!infile.eof()) 
         { 
            infile.getline(readLine,sizeof(readLine)); // read a line from file 
            student.insert(readLine); 
         } 
    }
    else
    {
        cout << "Can't open file!" << endl;
    }
}

最佳答案

我发现了我的问题。

在:

LList::LList()
{
    this -> head = new Node(0);
    mySize = 0;
    string data = "";
}

Node(0);

正在调用我

LList::Node::Node (string data_value)
{
    this -> data = data_value;
    this -> next = NULL;
}

初始化为字符串。

我变了

Node(0);

Node("");

它完美地工作。

关于c++ - 我收到错误 "invalid null pointer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22594936/

相关文章:

C++如何在字符后获取子字符串?

debugging - OpenCL 中错误代码的 perror() 等价物是什么?

debugging - Nestjs 如何在 VS 代码中编辑代码时进行调试

c++ - 未处理的异常错误、内存冲突

具有按签名和类型指向成员函数的指针的 C++ 模板

c++ - 跨 C++ 模块传递指向成员函数的指针

c++ - 是否可以在 Google Chrome 扩展程序中嵌入 HTTP 服务器?

javascript - 是什么导致我的 jQuery 调用出现这种递归?

c - 如何将数组传递给 C 中的方法并编辑内容?

c++ - “is not required” ==未定义行为?