Runtime error while trying to implement a Deque using Double Nodes in C++

  abstract-data-type, c++, deque, nodes, runtime-error

While trying to implement a deque by using double nodes my program began crashing. It does compile, yet a runtime error RUN FAILED (exit value -1.073.741.819, total time: 3s) occurs when I try to run it.

I started to comment lines of code to find which was causing the issue, and as it turns out multiple lines, seemingly unrelated, are all individually causing a runtime error. To elaborate, the error can be resolved by either

  1. Commenting LinkedDeque<int>* L = new LinkedDeque<int>(); in main.cpp, or replacing it by LinkedDeque<int>* L;,
  2. Commenting the second constructor of LinkedDeque as well as its destructor (in both LinkedDeque.cpp and LinkedDeque.h), or
  3. Commenting DoubleNode<T>* header; DoubleNode<T>* trailer; in DoubleNode.cpp as well as header->next = trailer; trailer->prev = header; inside the main constructor in LinkedDeque.cpp.

The worst part is that I don’t understand why either of the three cases above is causing a runtime error to begin with, and I was hoping someone could explain to me what I’m doing wrong. I’d appreciate any help.


DoubleNode.cpp:

#ifndef DOUBLENODE_H
#define DOUBLENODE_H

template <typename T>
class DoubleNode {
public:

    DoubleNode* next;
    DoubleNode* prev;
    T elem;

};

#endif /* DOUBLENODE_H */

LinkedDeque.h:

#ifndef LINKEDDEQUE_H
#define LINKEDDEQUE_H

#include "DoubleNode.h"


template <typename T>
class LinkedDeque {
public:

    LinkedDeque();
    virtual ~LinkedDeque(); //destructor
    LinkedDeque(const LinkedDeque& orig);

private: 

    DoubleNode<T>* header; 
    DoubleNode<T>* trailer; 

};
#endif /* LINKEDDEQUE_H */

LinkedDeque.cpp:

#include <iostream>
#include <cstdlib>
#include "LinkedDeque.h"

template <typename T>
LinkedDeque<T>::LinkedDeque() {

    header->next = trailer;
    trailer->prev = header;

}

template <typename T>
LinkedDeque<T>::LinkedDeque(const LinkedDeque& orig) { }

template <typename T>
LinkedDeque<T>::~LinkedDeque() { }

template class LinkedDeque<int>;

main.cpp:

#include <cstdlib>
#include "LinkedDeque.h"
using namespace std;

int main() {

    LinkedDeque<int>* L = new LinkedDeque<int>();

}

Source: Windows Questions C++

LEAVE A COMMENT