So right now I'm trying to implement a list using STL but I'm stuck at what to do for the print function (so I'm not sure how to test if the insertFront function works properly). I've searched for solutions but I'm having a difficult time understanding. I'm having some trouble using iterators in this implementation as well.
All the examples I've seen are implemented in a single file without needing function calls so this is extremely confusing for me.
Right now I'm getting an access violation error and am not sure how to properly initialize the pointer (assuming it's not done the same way as in an actual linked list with ptr = nullptr or something).
The function getNumOfElem() was already there as a working example. The only stuff I added are the functions other than that one.
Here's my code:
AnyList.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class AnyList
{
public:
AnyList();
int getNumOfElem() const;
void insertFront(int data);
void forwardPrint();
private:
list<int> *ptr;
};
The ptr member of AnyList is never assigned or initialized to any value, so it points to some random place in memory. You are trying to treat that random place as if it is a std::list. It isn't. Don't do that.