2012-11-09 132 views
2

我正在尝试为学校作业创建自己的哈希类,但在获得部分方法后,我被卡住了一个我无法调试的错误。调试Assestsion失败

当我运行我的项目时,出现“Debug Assertion Failed!... Expression:invalid null pointer”。

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

回答

4

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

不正确。这是正确的

MyHash::MyHash() 
{ 
} 

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

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

+0

当我删除我的构造函数的内容时,编译并正确运行。如果std :: string自动为NULL,那么assign(NULL)不应该改变任何东西并导致我的程序崩溃?! – Zzz

+0

std :: string是一个*空字符串*自动。我认为那是你想要做的。但是NULL和空字符串不是一回事。如果你用指针调用'assign',C++就希望这个指针指向一个C字符串,例如'学生[I] .name.assign( “约翰”)'。如果它的NULL值可能会崩溃。 – john

+0

@john你的回答是正确的 - 但值得注意的是NULL不能传递给'std :: string :: assign(const char *)'。你已经在评论中提到了这一点 - 但它应该是你核心答案的一部分,我相信这会帮助其他读者。 – PiotrNycz