Abd El Rahman Mohammed
Abd El Rahman Mohammed

Reputation: 107

c++ linked list inside another one

I'm trying to make a linked list inside another one, here is my code

template<typename T>
class List {
private:
    int length;
    class Node {
    public:
        T data;
        Node* next;
    } *head;

public:
    List();
    ~List();

    void insert(T item);
    void remove(T item);
    void empty();
    T* getAll();
    int count() const;       
};

template<typename T>
void List<T>::insert(T item)
{
    if (head == NULL)
    {
        head = new Node();
        head->data = item;
        head->next = NULL;
        length = 1;
        return;
    }
    Node* p = new Node();
    p->data = item;
    p->next = head;
    head = p;
    ++length;
}

struct Remainder {
    Date dt;
    List<int> notes;
};


void getDayEvents(Date dt, List<Remainder> l)
{
    Remainder* arr = new Remainder[l.count()];
    arr = l.getAll();
    for (int i = 0; i < l.count(); i++)
    {
        if (arr[i].dt.day == dt.day && arr[i].dt.month == dt.month &&              arr[i].dt.year == dt.year)
        {
            int* nArr = new int[arr[i].notes.count()];
            nArr = arr[i].notes.getAll();
            for (int j = 0; j < arr[i].notes.count(); j++)
            {
                cout << nArr[j] << endl;
            }
        }
    }
}

int _tmain() {
    Date dt1, dt2;
    dt1.setDate(17, 7, 2015);
    dt2.setDate(5, 11, 2015);

    Remainder r1, r2;
    r1.dt = dt1;
    r1.notes.insert(1);
    r1.notes.insert(2);
    r2.dt = dt2;
    r2.notes.insert(5);

    List<Remainder> l;
    l.insert(r1);
    l.insert(r2);

    getDayEvents(dt1, l);

    //----------------------------------------------------------
    int pause;
    cin >> pause;
    return 0;
}

just when insert r1 or r2 in the list, the data inside lists of notes inside each remainder just disappeared or destroyed

I don't know why? Where is the mistake?

Upvotes: 0

Views: 1131

Answers (1)

David K
David K

Reputation: 3132

You're passing your objects into the insert function by value. That means copy constructors and destructors are being called. You did not declare the copy constructor of List so it's presumably being generated by the compiler, and presumably is making a "shallow" copy of the List when the copy constructor of Remainder makes copies of the members of the input Remainder.

I think this copies the pointer head from one List to another so that now you have two List objects whose head pointers point to the same object. You haven't shown the definition of the List destructor, but depending on what you do in that destructor it could be deleting a Node that the destroyed List points to while another List still has a pointer to the same Node.

That may sound confusing, and frankly I'm not sure I can count the invocations of constructors and destructors correctly myself, so best to just make sure they can never be used unsafely.

A good start might be to define your own copy constructor and operator = for List in such a way that the new List has newly-allocated copies of everything that was in the old List. Never let two head pointers point to the same object. After you've inserted one List into another, you should be able to confirm in the debugger that the new copy of the List

Upvotes: 3

Related Questions