Alright, I implemented your code, and I'm not quite sure what went wrong, but doing it just as you had it, this code should work perfectly:
1 2 3 4 5 6 7 8 9
|
int main() {
vp::list<int> myList;
myList.push_back(5);
myList.push_back(6);
myList.push_back(5);
myList.push_back(7);
for (auto iter : myList)
std::cout << "Node: " << *iter << "\tValue: " << iter.value << "\n";
}
|
However, the first thing I notice is that I can't just display iter, I
need to dereference it. I didn't believe this to be the case with other iterators. My second thing I noticed is the output, as seen below. Something doesn't seem right, most likely in the ++ function. The only reason I was able to get this correctly was because I removed the ->next from the end() function. I would get an infinite loop and then the program would crash, which I guess was due to an access violation.
Node: 5 Value: 5
Node: 6841185 Value: 6841185
Node: 6 Value: 6
Node: 1163154768 Value: 1163154768
Node: 5 Value: 5
Node: 842879826 Value: 842879826 |
Obviously my list is slowly coming together (which makes me happy) but I am soon going to have to sit down and write this all out on paper again so I can completely understand everything that's going on.
Edit: Maybe I should have asked this as well, but what is lastNode->next supposed to equal? Currently, the lastNode->next points to a NULL node...but if that was the case, couldn't I just return NULL instead?