c++ - 调试断言失败

标签 c++ pointers data-structures assertions integer-hashing

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

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

程序在我的驱动程序文件 (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

最佳答案

这个

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

不正确。这是正确的

MyHash::MyHash()
{
}

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

事实上,您根本不需要 MyHash 构造函数,因为默认构造函数会执行您在编写的版本中尝试执行的所有操作。您会发现 C++ 比您想象的要容易。

关于c++ - 调试断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13315969/

相关文章:

c++ - 这是以前做过的吗? (用于链式操作的 Monad View 包装 C++ 集合/类型)

c - 为什么程序在 scanf 上停止...如果删除该行它可以正常工作

c - 我什么时候在 scanf() 中使用 "&"?

c - 不兼容的指针类型警告

c++ - 通过自定义分隔符从文件中读取对象数组

c++ - 类型和非类型模板特化

c++ - 如何安全地取消 Boost ASIO 异步接受操作?

algorithm - 从后缀树生成后缀

data-structures - Common Lisp 的优先级队列?

language-agnostic - 同义词字典实现?