c++ - 构造函数不更新类成员变量

标签 c++ class constructor hashtable

因此,我正在尝试实现一个哈希表,但我无法查看我的类或构造函数中的错误。总之,当我尝试访问哈希表数组的元素时,我可以在构造函数中,但我不能在成员函数中(我得到段错误),这让我相信我的类/构造函数没有问题工作。

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;
 }

这些函数做它们应该做的事情

但是当我运行这个函数时。我在 hashtable[place]==NULL 级别遇到段错误。

int website::insert(const mainentry& input)
{
    int place = 0;

    node*temp = new node;
     /* Ignore this part
    temp->data.topic = new char[strlen(input.topic)+1];
    strcpy(temp->data.topic, input.topic);

    temp->data.url = new char[strlen(input.url)+1];
    strcpy(temp->data.url, input.url);

    temp->data.summary = new char[strlen(input.summary)+1];
    strcpy(temp->data.summary, input.summary);

    temp->data.review = new char[strlen(input.review)+1];
    strcpy(temp->data.review, input.review);

    temp->data.rating = input.rating;
    */

     place = hashfunction(temp->data.topic);
    cout<<"Place is: "<<place<<endl; //Hash function works correctly
    if (hashtable[place]== NULL) // THIS IS THE PART I GET SEG FAULT
    {
            hashtable[place] = temp;
            temp->next = NULL;
            return 1;
    }
    else
    {
            temp->next = hashtable[place];
            hashtable[place] = temp;
            return 1;
    }
return 0;
}

这是我的类(class):

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;
};

我假设我犯了初学者的错误,但我看不出发生了什么,如果有人能指导我,我将不胜感激。

最佳答案

你在构造函数中隐藏了类的hashtable变量:

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.
}

关于c++ - 构造函数不更新类成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47219533/

相关文章:

c++ - 3ds Max 如何在网格类中存储法线?

c++ - 使用 SendInput 发送两个或多个字符

C++ 计数时间克服 clock_t 的 72 分钟范围

Java 类和构造函数

c++ - gcc -Wall 引入编译器错误

javascript - 在 css 工作表中填充编号的类名

c++ - 在类名之后但在开括号之前的引号

java - 添加与其他构造函数类似的构造函数意味着什么?

c++ - 在结构的 STL 映射中,为什么 "[ ]"运算符会导致结构的 dtor 被额外调用 2 次?

c++ - 接受字符串引用的构造函数。馊主意?