Question

I am attempting to create my own hash class for a school assignment, but after getting part of the way there I am stuck on an error that I haven't been able to debug.

When I run my project I get a "Debug Assertion Failed!...Expression: invalid null pointer."

The program is giving this error on line 16 in my driver file (test.cpp). It appears to me that the pointer to my class is not NULL.

Any help as to what causes this error and how I could fix it would be greatly appreciated.

//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
Was it helpful?

Solution

This

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

is incorrect. This is correct

MyHash::MyHash()
{
}

You don't have to 'assign NULL' to std::string. The default constructor for std::string automatically creates an empty string. In fact assigning NULL to a std::string is an error because this method always expects a pointer to a C string.

In fact you don't need a MyHash constructor at all, because the default one does everything you were trying to do in the version you wrote. You see C++ is easier than you think.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top