vendredi 25 novembre 2016

C++: Copy constructor crashing

I am having trouble coding Copy constructor for C++ HashTable. Now below is the class structure

 template <class TYPE>
class HashTable :public Table<TYPE>
{
      struct Record
            {
                TYPE data_;
                string key_;
                Record* Next;

                Record(const string& key, const TYPE& data)
                {
                    key_ = key;
                    data_ = data;
                    Next = nullptr;
                }
                Record(const Record& a)  {
                    if(!a.key_.empty()){

                if(a.Next == nullptr){
                    Next = nullptr;
                }
                else
                {
                Record* temp = a.Next ;
                while(temp != nullptr)
                {
                    Next = temp ;
                    temp = temp->Next ;
                }

                }

                data_ = a.data_ ;
                key_ = a.data_ ;
        } 
    }

            };
            int TableSize;
            Record** records;
}

and below is the copy constructor template

HashTable<TYPE>::HashTable(const HashTable<TYPE>& other)
{
   records = new Record*[other.TableSize] ;
   TableSize = other.TableSize ;
   for(int i = 0 ; i < other.TableSize; i++)
    records[i]= (new Record(*other.records[i]));

 }

I have also posted the code on iodine http://ift.tt/2gdzQXO. The code for copy constructor seems to be crashing. I don't see any memory leak that will cause the program to crash. I have tried memcopy, using the insert function and the all seems to fail.

Aucun commentaire:

Enregistrer un commentaire