gpt4 book ai didi

c++ - 哈希表段错误

转载 作者:行者123 更新时间:2023-11-28 02:28:05 25 4
gpt4 key购买 nike

所以我真的不确定是什么导致了 seg 错误,但我感觉它与 hashTables 有关,因为调试器在它涉及的这一行显示它的 seg 错误。

这是导致段错误的代码

//Constructor for hashtable
HashTable::HashTable()
{
for(int i = 0; i < 10; i++)
{
hashTable[i] = new Movie;
hashTable[i]->title = "empty";
hashTable[i]->year = 0;
hashTable[i]->next = NULL;
}
}

//destructor for hashtable
HashTable::~HashTable()
{

}

// this will take a string and convert the letters to hash numbers then add them all together and divide by the hash table size to get a index
int HashTable::initHash(std::string in_title)
{
int hashT = 0;
int index = 0;

for(int i = 0; i < in_title.length(); i++)
{
hashT = hashT + (int)in_title[i];
std::cout << "hash = " << hashT << std::endl;
}

index = hashT % 10;
std::cout << "index = " << index << std::endl;

return index;
}

//This is where we will be inserting a new Movie into the hashtable,
//it will first use initHash to find the number of where it should go in the hash and then from there add it to the hashtable
void HashTable::insertMovie(std::string in_title, int year)
{
int index = initHash(in_title);
std::cout << "index = " << index << std::endl;

if (hashTable[index]->title == "empty") // *** seg faults right here ***
{
hashTable[index]->title = in_title;
hashTable[index]->year = year;
}

else
{
Movie* Ptr = hashTable[index];
Movie* n = new Movie;
n->title = in_title;
n->year = year;
n->next = NULL;
while(Ptr->next != NULL)
{
Ptr = Ptr->next;
}
Ptr->next = n;
}
}

在我的每个包含 hashTables[index] 的函数中,它都会出现错误,但我不确定为什么,索引会返回一个应该有效的数字。有谁知道为什么会这样?

编辑:好的,这是我的头文件中重要的两个类

struct Movie{
std::string title;
int year;
Movie *next;

Movie(){};

Movie(std::string in_title, int in_year)
{
title = in_title;
year = in_year;
}

};

class HashTable
{
public:
HashTable();
~HashTable();
void insertMovie(std::string in_title, int year);
int initHash(std::string in_title);
int NumberofItemsInIndex(int index);
Movie* findMovie(std::string in_title/*, int *index*/);
void deleteMovie(std::string in_title);
void printInventory();
void PrintItemsInIndex(int index);
protected:
private:
Movie **hashTable;
};

编辑 2:这是 main() 函数

int main(int argc, char * argv[])
{
// Declarations
int input; // Declaring an input for the menu
bool quit = false; // Bool for the menu
//string title; // input value for certain actions
//int year; // input value for certain actions

HashTable *ht;
//int index;

//readFileIntoHash(ht, argv[1]);



while(quit != true)
{
displayMenu(); // Displays the main menu
cin >> input;

//clear out cin
cin.clear();
cin.ignore(10000, '\n');

switch (input)
{

// Insert a movie
case 1:
{
string in_title;
int year;
cout << "Enter Title:" << endl;
cin >> in_title;
cout << "Enter Year:" << endl;
cin >> year;
ht -> insertMovie(in_title, year);
break;
}

// Delete a movie
case 2:
{
string in_title2;
cout << "Enter Title:" << endl;
cin >> in_title2;
ht -> deleteMovie(in_title2);
break;
}

// Find a movie
case 3:
{
string in_title3;
cout << "Enter Title:" << endl;
cin >> in_title3;
ht -> findMovie(in_title3);
break;
}

// Print table contents
case 4:
ht -> printInventory();
break;

case 5:
cout << "Goodbye!" << endl;
quit = true;
break;

// invalid input
default:
cout << "Invalid Input" << endl;
cin.clear();
cin.ignore(10000,'\n');
break;
}
}
return 0;
}

void displayMenu()
{
cout << "======Main Menu=====" << endl;
cout << "1. Insert movie" << endl;
cout << "2. Delete movie" << endl;
cout << "3. Find movie" << endl;
cout << "4. Print table contents" << endl;
cout << "5. Quit" << endl;
return;
}

为了清楚起见,我添加了 displayMenu()。

最佳答案

修改类定义:

class HashTable
{
public:
HashTable();
~HashTable();
void insertMovie(std::string in_title, int year);
int initHash(std::string in_title);
int NumberofItemsInIndex(int index);
Movie* findMovie(std::string in_title/*, int *index*/);
void deleteMovie(std::string in_title);
void printInventory();
void PrintItemsInIndex(int index);
protected:
private:
Movie *hashTable[10]; /*<<-- since your size is fixed use an array*/
};

另外..因为你在构造函数中分配了电影,记得在析构函数中释放它们:

//destructor for hashtable
HashTable::~HashTable()
{
for(int i = 0; i < 10; i++)
{
delete hashTable[i];
}
}

替代方案,使用动态分配的内存(使用相同的类定义)

HashTable::HashTable()
{
hashTable = new (Movie*) [10];
for(int i = 0; i < 10; i++)
{
hashTable[i] = new Movie;
hashTable[i]->title = "empty";
hashTable[i]->year = 0;
hashTable[i]->next = NULL;
}
}

HashTable::~HashTable()
{
for(int i = 0; i < 10; i++)
{
delete hashTable[i];
}
delete[] hashTable;
}

附加修复:

修改主函数:

int main(int argc, char * argv[])
{
// Declarations
int input; // Declaring an input for the menu
bool quit = false; // Bool for the menu
//string title; // input value for certain actions
//int year; // input value for certain actions

HashTable ht; // <<==== local variable, not a pointer!

... 然后将 ht->xxxx(...) 替换为 ht.xxxx(...) 其他地方。

关于c++ - 哈希表段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com