mercredi 4 mars 2020

double free or corruption (fasttop) (doubly linked list c++)

I'm attempting to create a doubly linked list with an iterator container. Declaring a list gives me the error of: Error in `mydriver': double free or corruption (fasttop). To be frank, I have no idea what to do. I'm working off an assignment that I really don't understand, so any help or insight would be amazing, I have no idea what I'm doing. Thank you guys!

Here is my tlist.h file:

#ifndef TLIST_H
#define TLIST_H
#include <iostream>
#include <utility>
#include "tnode.h"
// **********************************************************
// * Name:  tlist.h.                                        *
// * Description:   Starter file for doubly-linked list.    *
// *    This is a templated list class designed to use the. *
// *    node class to store multiple occurances of the data *
// *    of type T.                                          *
// * Last Modifed by: Dr. David A. Gaitros                  *
// * Date: 12/8/2019                                        *
// **********************************************************

template <typename T>
class TList
{
   friend class TListIterator<T>;


public:
   TList();                             // Create empty linked list
   TList(T val, int num);               // Create list with num copies of val
   ~TList();                            // Destructor
   TList(const TList& L);               // Copy constructor
   TList& operator=(const TList& L);    // Copy assignment operator
   TList(TList && L);                   // Move constructor
   TList& operator=(TList && L);        // Move assignment operator

   bool IsEmpty() const;                // Checks to see whether list is empty
   void Clear();                        // Clear out list, reset to empty
   int GetSize() const;                 // Return the size of the list

   void MyPrint();
   void InsertFront(const T& d);        // Insert data d as first element
   void InsertBack(const T& d);         // Insert data d as last element
   void RemoveFront();                  // Remove first element of list
   void RemoveBack();                   // Remove last element of list

   T& GetFirst() const;                 // Return first element of list
   T& GetLast() const;                  // Return last element of list

   TListIterator<T> GetIterator() const;    // Return iterator to first item
   TListIterator<T> GetIteratorEnd() const; // Return iterator to last item
// **********************************************************
// * Insert data element d just before item at pos position *
// **********************************************************

   void Insert(TListIterator<T> pos, const T& d);
// **********************************************************
// * Remove data item at position pos. Return iterator      *
// *  to the item.                                          * 
// *  that comes after the one being deleted.               *
// **********************************************************

   void Insert(TListIterator<T> pos, const T& d);
// **********************************************************
// * Remove data item at position pos. Return iterator      *
// *  to the item.                                          * 
// *  that comes after the one being deleted.               *
// **********************************************************

   TListIterator<T> Remove(TListIterator<T> pos);
// **********************************************************
// * Print list contents in order, separated by given       *
// * delimiter.                                             *
// **********************************************************

   void Print(std::ostream& os, char delim = ' ') const;

private:
   Node<T>* first;              // pointer to first node in list
   Node<T>* last;               // pointer to last node in list
   int size;                    // number of nodes in the list
   static T dummy;              // dummy object, for empty list data returns
                                //  assuming type T has default construction

};
#endif

template <typename T>
T TList<T>::dummy;              // initialization of static member
// **********************************************************
// *. Stand-alone function for concatenating two TList      *
// *  objects                                               *
// **********************************************************

template <typename T>
TList<T> operator+(const TList<T>& t1, const TList<T>& t2);
// **********************************************************
// Declaration of class TListIterator                       *
// **********************************************************


template <typename T>
class TListIterator
{
   friend class TList<T>;

public:
   TListIterator();                     // default constructor
   bool HasNext() const;                // next item available?
   bool HasPrevious() const;            // previous item available?
   TListIterator<T> Next();             // advance to next item
   TListIterator<T> Previous();         // move to previous item
   T& GetData() const;                  // return data element of current node

private:
   Node<T>* ptr;                // pointer to current list item
};


Here is the tlist.hpp file:

#include <string>
#include <iostream>
#include "tlist.h"
using namespace std;

template <typename T>
TList<T>::TList()
{
first = new Node<T>(-1);
last = new Node<T>(999999);

first->next=last;
first->prev = nullptr;
last->next = nullptr;
last->prev = first;

}

template <typename T>
TList<T>::TList(T val, int num)
{
first = new Node<T>(-1);
last = new Node<T>(999999);

first->next=last;
first->prev = nullptr;
last->next = nullptr;
last->prev = first;


for(int i=0; i<num; i++)
        {
        Node<T> * node;
        node->next=first->next;
        node->prev=(first->next)->prev;
        first->next=node;
        (first->next)->prev=node;
        }
}

template<typename T>
void TList<T>::Print(std::ostream& os, char delim) const
{
Node<T> * search;
search = first->next;

while(search!=last)
        {
        os<< search->data<<endl;
        search = search->next;
        }

}
template<typename T>
void TList<T>::MyPrint()
{
Node<T> * search;
search = first->next;

while(search!=last)
        {
        cout<< search->data<<endl;
        search=search->next;
        }
cout<<"were done slut"<<endl;
}

Here is the tnode.h file:

// **********************************************************
// * Name:  tnode.h.                                        *
// * Description:   Starter file for doubly-linked list.    *
// *    This is a templated Node class designed for         *
// *    each Node to store a single piece of data, along    *
// *    with prev and next pointers that point to the       *
// *    previous and next record in the list.               *
// * Last Modifed by: Dr. David A. Gaitros                  *
// * Date: 12/8/2019                                        *
// **********************************************************
#ifndef TNODE_H
#define TNODE_H
#include <utility>
#include <iostream>
using namespace std;

template <typename T>   class TList;            // forward declaration
template <typename T>   class TListIterator;    // forward declaration


template <typename T>
class Node
{
   friend class TList<T>;
   friend class TListIterator<T>;
public:
   Node(const T& d);
   Node(const T&& d);

private:
   T data;              // data element to store of type T
   Node<T> * prev;      // pointer to previous node
   Node<T> * next;      // pointer to next node
};

#endif


template <typename T>
// **********************************************************
// * Node Copy Constructor.   Pass a node of type T during  *
// * declaration of another Node to copy the contents of    *
// * the data passed in to the node declared.               *
// **********************************************************

Node<T>::Node(const T& d)

{
   data = d;                            // set data
   prev = next = nullptr;               // null pointers to start
}
// **********************************************************
// * Basic constructor.  Creates a node with data of type T *
// **********************************************************


template <typename T>
Node<T>::Node(const T&& d)

{
   data = std::move(d);                 // set data
   prev = next = nullptr;               // null pointers to start
}

Here is mydriver file:

#include <iostream>
#include <string>
#include "tlist.hpp"

using namespace std;

int main(void)
{
 cout<<"Please just work";
TList<int> L1;

L1.MyPrint();


return 0;
}

Finally here is my makefile as I had alot of trouble with that as well:

mydriver: mydriver.o
        g++ -o mydriver -std=c++11 mydriver.o
mydriver.o: mydriver.cpp tlist.hpp
        g++ -c -std=c++11 mydriver.cpp tlist.hpp
clean:
        -rm *.o
        -rm *.h.gch


Again guys, any help would really be amazing! I've been trying to figure this out for 10h today and I've made very little progress, I really appreciate you guys!

Aucun commentaire:

Enregistrer un commentaire