I'm trying to make a list of objects (not using std::list
) containing a pointer to the next and previous objects. For some reason the following code is throwing a segmentation fault, but when I commented out std::cout
the code won't throw a segmentation fault, and when I don't compile with cmake, but with clang++. In both cases I'm using C++14.
#include <iostream>
class myListElement
{
myListElement *next;
double val;
public:
myListElement(int entry, myListElement *newPrev):val(entry), prev(newPrev){}
void setNext(myListElement *newPrev){prev = newPrev;}
};
class myList
{
myListElement *first,*last;
public:
myList(){}
~myList(){}
void push_back(int entry)
{
myListElement temp(entry,last);
if(last != nullptr)
{
last->setNext(&temp);
}
}
};
int main()
{
int n = 1000;
myList my_list;
//std::cout << "\ntest";
for(int i = 0; i < n; ++i)
{
my_list.push_back(i+1);
}
}
How can that be the case?
I'm sorry for the long code, but I couldn't find any part to delete without getting the Segmentation fault and keeping the sense of the program. Thank you for every bit of help!