C++ 调试断言失败

C++ 调试断言失败,c++,pointers,data-structures,assertions,integer-hashing,C++,Pointers,Data Structures,Assertions,Integer Hashing,我正试图为学校作业创建自己的哈希类,但在完成部分任务后,我遇到了一个无法调试的错误 当我运行我的项目时,我得到一个“调试断言失败!”…表达式:无效的空指针 程序在我的驱动程序文件(test.cpp)的第16行给出了这个错误。在我看来,指向我的类的指针不是NULL 如果您能帮我解决这个错误,我将不胜感激 //Hash.h #include <string> struct Student { std::string name; std::string id; }; c

我正试图为学校作业创建自己的哈希类,但在完成部分任务后,我遇到了一个无法调试的错误

当我运行我的项目时,我得到一个“调试断言失败!”…表达式:无效的空指针

程序在我的驱动程序文件(test.cpp)的第16行给出了这个错误。在我看来,指向我的类的指针不是
NULL

如果您能帮我解决这个错误,我将不胜感激

//Hash.h

#include <string>
struct Student
{
    std::string name;
    std::string id;
};

class MyHash{
public:
    MyHash();
    int hashCode(int, int);
    void insertItemCollision(std::string, std::string);
    Student retrieveItem(std::string);
    Student students[100];
};

//Hash.cpp
#include <iostream>
#include "Hash.h"


MyHash::MyHash()
{
    for(int i = 0; i < 100; i++)
    {
        students[i].id.assign(NULL);    
        students[i].name.assign(NULL);
    }
}



int MyHash::hashCode(int id, int max)
{

  return (id % max);
}

void MyHash::insertItemCollision(std::string id, std::string name)
{
    int idInt = atoi(id.c_str());
    int location;


  location = hashCode(idInt, 100);
  while (students[location].id != "")
    location = (location + 1) % 100;
  students[location].id = id;
  students[location].name = name;
}


Student MyHash::retrieveItem(std::string id)
{
  int location;
  int startLoc;
  int idInt = atoi(id.c_str());
  bool moreToSearch = true;
  bool found;
  Student item;

  startLoc = hashCode(idInt, 100);
  location = startLoc;
  do
  {
      if (students[location].id == id || students[location].id == "")
      moreToSearch = false;
    else
      location = (location + 1) % 100;
  } while (location != startLoc && moreToSearch);
  found = (students[location].id == id);
  if (found)
    item = students[location];
  return item;
}


//test.cpp
#include <iostream>
#include<string>
#include<fstream>
#include "Hash.h"
using namespace std;


int read(string[]);
void splitString(string, Student&);
void init(string[], Student[], int);

int main()
{
    int size;
    string input[100];
    MyHash* h = new MyHash();
    size = read(input); 
    init(input, h->students, size);

    system("pause");
    return 0;
}

int read(string st[])
{
    int size = 0;
    ifstream infilestream;
    infilestream.open("test.txt");


    for(int i = 0; infilestream.good(); i++)
    {
        getline(infilestream, st[i]);
        cout<<st[i] <<endl;
        size++;
    }
    infilestream.close();
    return size;
}

void splitString(string record, Student& s)
{
    s.id = record.substr(0, 4);
    s.name = record.substr(5, record.length());
}

void init(string inputs[], Student stus[], int size)
{
    for(int i = 0;i < size; i++)
    {
        splitString(inputs[i],stus[i]);
        cout << stus[i].name << " " << stus[i].id << endl;
    }
}

//test.txt
9892 Zack Lewis
4592 Ken Rodriguez
9819 Anderson Clark
1519 Ben Robinson
4597 Abigail Martinez
8542 Madison Garcia
6113 Mia Thompson
8591 Chloe Martin
9491 Daniel Harris
1698 Aiden White
5984 Alexander Walker
6541 Ethan Jackson
9549 Michael Thomas
5949 Emily Anderson
9861 Ava Taylor
5412 Noah Moore
6262 Olivia Wilson
1954 Jayden Miller
4954 William Davis
9567 Emma Brown
5195 Mason Jones
9195 Isabella Williams
5199 Sophia Johnson
1294 Jacob Smith
//Hash.h
#包括
体类型
{
std::字符串名;
std::字符串id;
};
类MyHash{
公众:
MyHash();
int hashCode(int,int);
void insertItemCollision(std::string,std::string);
学生检索项目(std::string);
学生[100];
};
//Hash.cpp
#包括
#包括“Hash.h”
MyHash::MyHash()
{
对于(int i=0;i<100;i++)
{
学生[i].id.assign(空);
学生[i].name.assign(空);
}
}
int-MyHash::hashCode(int-id,int-max)
{
返回(id%max);
}
void MyHash::insertItemCollision(std::string id,std::string name)
{
int idInt=atoi(id.c_str());
int定位;
位置=哈希代码(IDIT,100);
while(学生[location].id!=“”)
位置=(位置+1)%100;
学生[location].id=id;
学生[地点]。姓名=姓名;
}
学生MyHash::retrieveItem(std::string id)
{
int定位;
国际奥委会;
int idInt=atoi(id.c_str());
bool moreToSearch=真;
布尔发现;
学生项目;
STARTOC=哈希代码(IDIT,100);
位置=摄氏度;
做
{
if(学生[位置].id==id | |学生[位置].id==“”)
moreToSearch=假;
其他的
位置=(位置+1)%100;
}while(位置!=startoc&&moreToSearch);
找到=(学生[location].id==id);
如果(找到)
项目=学生[地点];
退货项目;
}
//test.cpp
#包括
#包括
#包括
#包括“Hash.h”
使用名称空间std;
整数读取(字符串[]);
无效拆分字符串(字符串、学生和);
void init(字符串[],学生[],整数);
int main()
{
整数大小;
字符串输入[100];
MyHash*h=新的MyHash();
大小=读取(输入);
初始(输入,h->学生,大小);
系统(“暂停”);
返回0;
}
整型读取(字符串st[])
{
int size=0;
ifstream;
infirestream.open(“test.txt”);
for(int i=0;infirestream.good();i++)
{
getline(Insidestream,st[i]);
不能

您不必将NULL“赋值”给
std::string
std::string
的默认构造函数会自动创建一个空字符串。事实上,将NULL赋值给std::string是一个错误,因为此方法始终需要指向C字符串的指针

实际上,你根本不需要一个<代码> MyHase< /Cuff>构造函数,因为默认的一个你在你所写的版本中做的所有事情。你看C++比你想象的要容易。

< P>这个< /P> 您不必将NULL“赋值”给
std::string
std::string
的默认构造函数会自动创建一个空字符串。事实上,将NULL赋值给std::string是一个错误,因为此方法始终需要指向C字符串的指针


实际上,你根本不需要一个<代码> MyHase< /Cuff>构造函数,因为默认的一个你在你所写的版本中做的所有事情。你看到C++比你想象的要容易。< /P>当我删除了我的构造函数的内容,所有的东西都被编译好并正常运行。如果STD::String是空的,然后自动分配(null)。不应该改变任何东西,导致我的程序崩溃。String是一个空字符串。我假设这就是你想要做的。但是NULL和空字符串不是一回事。如果用指针调用<代码>赋值< /C>,C++希望指针指向C字符串,例如“代码>学生[i]。

。如果为NULL,则可能会发生崩溃。@john您的答案是正确的-但值得注意的是,NULL不能传递给
std::string::assign(const char*)
。您已经在评论中提到了这一点,但这应该是您核心答案的一部分,我相信这将帮助其他读者。当我删除构造函数的内容时,所有内容都已正确编译并运行。如果std::string自动为NULL,则分配(NULL)不应该改变任何东西,导致我的程序崩溃。String是一个空字符串。我假设这就是你想要做的。但是NULL和空字符串不是一回事。如果用指针调用<代码>赋值< /C>,C++希望指针指向C字符串,例如“代码>学生[i]。。如果为NULL,您可能会崩溃。@john您的答案是正确的-但值得注意的是,NULL不能传递给
std::string::assign(const char*)
。您已经在评论中提到了这一点-但它应该是您核心答案的一部分,我相信这将帮助其他读者。
MyHash::MyHash()
{
    for(int i = 0; i < 100; i++)
    {
        students[i].id.assign(NULL);    
        students[i].name.assign(NULL);
    }
}
MyHash::MyHash()
{
}