Linked List Problem

Started by
7 comments, last by GameDev.net 17 years, 10 months ago
I am new to c++ and i wana make a pathfinding program anyway i want to save positions of the map on a linked list. So i wrote this: typedef class anlist//ALPHANUMERIC LIST { public: anlist() { pointer = 0; _next = 0; x = y = 0; } void add() { if(_next == 0) { _next = new anlist; } else { cout << "?"; } } ~anlist() { if(pointer != 0) { delete [] pointer; } if(_next != 0) { delete [] _next; } } unsigned short x,y; private: char *pointer; anlist *_next; }ANLIST; The problem is that when i try to add() a bode to the list it says Debig Assseretion Failed! and an expression _BLOCK_TYPE_IS_VALID(pHEAD->nBlockinuse). But that just doesent make sense !? Any reply would be apriciated
Advertisement
If you're using new (rather than new[]), you need to use delete rather than delete[].
Thanks bakery2k1 cause that was it :S.lol
The real problem seems to be that you're not using the STL (std::list) :)
I am using namespaces :|
Isnt that the same ?
delete & delete[] both check for null-ness, you're just adding one more check like this:
if (p != NULL) {    // delete {    if (p != NULL) {        ...    } // } delete}


EDIT: The A.P. meant that you should be using std::list:
#include <list>using std::list;int main() {    std::list<int> l;    l.push_back(1);    l.push_front(0);    l.push_front(123);    l.pop_front();    // should be:    // 0 1}
Well i got that about delete but about the stl ?
My list is a cust one i am not using list.h.
The std::list<int> l;
Doesent mean that list is a class template ?
and i have wrote using namespace std then the std:: would not be necesary,would it ?

PS:How do you put the source code in a box ?
Code in a box in source /source tags, each in square brackets.

They mean you "should" use std::list in the sense that it is better to use the tried and tested, writted by experts list class rather than write you own.

Writing your own is a very good learning technique though. Just don't do it in any critical, team-based or life-critical applications!
Quote:Original post by DigitalBeing
My list is a cust one i am not using list.h.
The std::list<int> l;
Doesent mean that list is a class template ?


Yes, it's a templated class. Hence, you can use it for any type you want, including your own one.

Quote:Original post by DigitalBeing
and i have wrote using namespace std then the std:: would not be necesary,would it ?


You are correct.

This topic is closed to new replies.

Advertisement