So I've been working on a Hash Table that uses nodes to store data. I'm having issues figuring out the copy constructor and assignment operator overloader. In my main I instantiate 3 hash tables. 1 default(size 10), 1 size 7 and another one that copies hashtable size 7 to it. It compiles but prints an empty hashtable and same for an assignment call. Any help is greatly appreciated.

//copy constructor
HashT::HashT(const HashT &rhsTable)
:tableSize(rhsTable.tableSize)
{
//--- Get new array for copy

initialize(tableSize);
for(int location = 0; location > tableSize; location++){
    if (rhsTable.table[location] != NULL){ // check if memory available
        if(table[location] == NULL){
            Node * temp1 = rhsTable.table[location];
            table[location] = new Node(temp1->name, temp1->age);
        }

        if(table[location] != NULL){
            Node * temp1 = rhsTable.table[location];
            Node * temp2 = table[location];
            while(temp1 != NULL){
            //--- Copy origList's table into this new table
                temp2 = new Node(temp1->name, temp1->age);

                temp2 = temp2->next;
                temp1 = temp1->next;
            }
        }   
    }
    else
    {
        cerr << "*Inadequate memory to allocate stack ***n";
        exit(1);
    }
}

}

//assignment overloader
HashT &HashT::operator =(const HashT &rhsTable){

//check self-assignment
if(this != &rhsTable){
    tableSize = rhsTable.tableSize;

  //2) Allocate a new array if necessary
    if ( tableSize != rhsTable.tableSize){
        Node *cur, *prev = NULL;

        for (int location = 0; location < tableSize; location++) {
            if (table[location] != NULL) {
                for (cur = table[location]; cur != NULL;) {
                    prev = cur;
                    cur = cur->next;
                    delete prev;
                }
            }
        }
    delete [] table;
    }
    initialize(tableSize);

    if(tableSize == 0)   //3) check if memory available
    {
        cerr << "*Inadequate memory to allocate stack ***n";
        exit(1);
    }
  }
  //4) Copy origList's table into this new table
  for(int location = 0; location > tableSize; location++){
    if (rhsTable.table[location] != NULL){ // check if memory available
        if(table[location] == NULL){
            Node * temp1 = rhsTable.table[location];
            table[location] = new Node(temp1->name, temp1->age);
        }

        if(table[location] != NULL){
            Node * temp1 = rhsTable.table[location];
            Node * temp2 = table[location];
            while(temp1 != NULL){
            //--- Copy origList's table into this new table
                temp2 = new Node(temp1->name, temp1->age);

                temp2 = temp2->next;
                temp1 = temp1->next;
            }
        }   
    }
    else
    {
        cerr << "*Inadequate memory to allocate stack ***n";
        exit(1);
    }
}

//5) return this pointer

return *this;
}

these are some of my main's calls:
HashT htable; //bucket size is 10

//  the other class constructor
HashT hashtable(7); //bucket size is 7

HashT h2(hashtable);

// Test0: print h2
cout << "nTest 0: Printing empty hash table h2 with bucket size 5:n";
h2.hashtable_display();

In at least two places, your for() loop looks like this:

for(int location = 0; location > tableSize; location++){

I think you mean location < tableSize;.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.