C++ 构造函数不';t更新类成员变量
因此,我试图实现一个哈希表,但我很难看到我的类或构造函数中有什么错误。总之,当我尝试访问哈希表数组的一个元素时,我可以在构造函数中使用,但在成员函数中不能使用(我得到seg fault),这使我相信我的类/构造函数有问题,无法工作C++ 构造函数不';t更新类成员变量,c++,class,constructor,hashtable,C++,Class,Constructor,Hashtable,因此,我试图实现一个哈希表,但我很难看到我的类或构造函数中有什么错误。总之,当我尝试访问哈希表数组的一个元素时,我可以在构造函数中使用,但在成员函数中不能使用(我得到seg fault),这使我相信我的类/构造函数有问题,无法工作 website::website(int input) //Constructor { SIZE = input; node** hashtable = new node * [SIZE]; for (int i = 0; i
website::website(int input) //Constructor
{
SIZE = input;
node** hashtable = new node * [SIZE];
for (int i = 0; i<SIZE; i++)
{
hashtable[i] = NULL;
if(!hashtable[i])
{
cout<<"It works at "<<i<<"th"<<endl;//This is to check
}
}
}
int website::hashfunction(const char array []) //Hash function
{
int inputsize = strlen(array);
int value = 0;
for (int i=0; i< inputsize; i++)
{
value = value + int(array[i]);
}
value = value % SIZE;
return value;
}
这是我的班级:
class website
{
public:
website(int input);
// ~website();
int insert(const mainentry & input);
int retrieve( char [], mainentry output [] );
int edit (mainentry & input);
int remove();
int display(char []);
int display_all();
int hashfunction(const char []);
private:
int SIZE;
node ** hashtable;
};
我假设我犯了初学者的错误,但我看不出发生了什么,如果有人能指点我,我将不胜感激
节点**哈希表=新节点*[SIZE]代码>
应该是
hashtable=新节点*[SIZE]
您正在通过编写以下命令来隐藏构造函数中类的哈希表
变量:
website::website(int input) //Constructor
{
SIZE = input;
node** hashtable = new node * [SIZE]; //<<-- Shadowing. you are declaring a local scope veriable called hastable, and not using the class's instance.
}
website::website(int输入)//构造函数
{
大小=输入;
node**hashtable=new node*[SIZE];//使用std::vector而不是new[]!@name不允许在此类中使用向量或字符串:/
website::website(int input) //Constructor
{
SIZE = input;
node** hashtable = new node * [SIZE]; //<<-- Shadowing. you are declaring a local scope veriable called hastable, and not using the class's instance.
}