c++ how to return a pointer of a hashtable item

  c++, data-structures, hashtable, pointers

I apologize if this is a duplicate of any question. I’m trying to code a function that finds a key for me. I have two classes declared in separate header files. I’m having trouble in the find function.

Class Table is declared in htable.h as such:

template<typename X, typename Y>
class Table{
   public:
      X key;
      Y value;

Table<X,Y>(const X& input, const Y& input2){
     key = input;
     value = input2;
}

class Hash is declared in hashing.h as such:
template<typename X, typename Y>

class Hash{
    public:
      Hash(a = 80){
        size = a;
        elem = 0;
        table = new Table<X,Y>[80];
      };

    //unimportant functions

    Table<X,Y>* findKey(const X& keyinput);

private:
    Table<X,Y>* table;
    int size, elem;

    int function(const X& key input){
    //hash function
    }

My findKey should return a pointer to the item in the hash table, but I do not know how and I keep getting segmentation errors.

Table<X,Y>* findKey(const X& keyinput){
    int index = function(keyinput);
    //this is where I think I'm getting errors
    if(table[index].key == keyinput){
        return &table[index]; //I do not think I'm returning this correctly
    }

Since table is already a pointer, I assumed that I could dereference it and return it, but I keep getting segmentation errors. I’m not sure what specifically the error is I just get "segmentation fault"

Source: Windows Questions C++

LEAVE A COMMENT