I know exactly where this is error is being caused, and I think I might even have a clue as to why—but I can't figure out what's doing it:
Room
// room.hpp
#include "linkedlist.hpp"
#include "item.hpp"
class item;
class Room {
public:
Room();
private:
LinkedList<Item> itemsInThisRoom_;
};
-
// room.cpp
#include "room.hpp"
#include <iostream>
Room::Room() {
itemsInThisRoom_ = LinkedList<Item>();
};
LinkedList
// linkedlist.hpp
//====================
// Class (Declaration)
template<class T> struct LinkedList {
public:
LinkedList();
~LinkedList();
private:
Node<T>* head_;
};
//====================
// Class (Instantiation)
template<class T> LinkedList<T>::LinkedList() {
head_ = new Node<T>;
size_= 0;
};
template<class T> LinkedList<T>::~LinkedList() { delete head_; };
main.cpp
#include <iostream>
#include "room.hpp"
#include "linkedlist.hpp"
int main() {
Room room;
return 0;
};
The error is being called when Room's destructor is called—and only if I initialize a room object. This works fine:
#include <iostream>
#include "room.hpp"
#include "linkedlist.hpp"
int main() {
Room* room;
return 0;
};
Also, I can construct/destruct instances of LinkedList by itself without problem. I think that somehow 'head_' is being deleted twice, but I can't figure out how.
(Someone's advised me to start using std::unique_ptr instead of new/delete, which I will start doing, but I just want to figure out why this is causing an error!!)