Templates Help?
-
The following code is a very simple linked list it seems to work fine it compiles it runs and the output is the desierd output but the problem i'm having is in this line of code
if(m_cNext) { delete( m_cNext ); m_cNext = NULL; }
of the Reset member of the template. It Gives me an error on debug but not on release. Access violation writing location 0x00030fd4. I'm simply not seeing the problem any suggestions help would be helpful.#include< windows.h > #include< iostream.h > /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Linked List %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ template < typename xType > class CLinked { public: CLinked(); ~CLinked(); void SetCallDelete(BOOL b) { m_bCallDelete = b; if( m_Next ) m_Next->SetCallDelete(b); } xType* GetObject() { return(m_ObjData); } CLinked* GetNext() { return(m_cNext); } CLinked* GetPrev() { return(m_cPrev); } CLinked* AddObject(xType* pvPtr,CLinked* prev=NULL); void ReplaseObject(xType* obj) { m_ObjData = obj; } void Reset(); private: xType* m_ObjData; CLinked* m_cPrev; CLinked* m_cNext; BOOL m_bCallDelete; }; template < typename xType > CLinked< xType >::CLinked() { m_bCallDelete = TRUE; m_cPrev = NULL; m_cNext = NULL; m_ObjData = NULL; } template < typename xType > CLinked< xType >::~CLinked() { if( m_bCallDelete ) { if( m_ObjData ) delete( m_ObjData ); m_ObjData = NULL; } if(m_cNext) { delete( m_cNext ); m_cNext = NULL; } } template < typename xType > void CLinked< xType >::Reset() { if( m_bCallDelete ) { if( m_ObjData ) delete( m_ObjData ); m_ObjData = NULL; } if(m_cNext) { delete( m_cNext ); m_cNext = NULL; } m_cPrev = NULL; m_cNext = NULL; m_ObjData = NULL; } template < typename xType > CLinked< xType >* CLinked< xType >::AddObject(xType* pvPtr, CLinked* cpPrev) { // if NULL fail if( pvPtr == NULL ) return( NULL ); // if current has no obj store it if( m_ObjData == NULL ) { m_ObjData = pvPtr; m_cPrev = cpPrev; return( this ); } if( m_cNext ) { return( m_cNext->AddObject(pvPtr,this) ); } m_cNext = new CLinked(); if( m_cNext == NULL ) return(NULL); return( m_cNext->AddObject(pvPtr,this) ); } typedef CLin
-
The following code is a very simple linked list it seems to work fine it compiles it runs and the output is the desierd output but the problem i'm having is in this line of code
if(m_cNext) { delete( m_cNext ); m_cNext = NULL; }
of the Reset member of the template. It Gives me an error on debug but not on release. Access violation writing location 0x00030fd4. I'm simply not seeing the problem any suggestions help would be helpful.#include< windows.h > #include< iostream.h > /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Linked List %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ template < typename xType > class CLinked { public: CLinked(); ~CLinked(); void SetCallDelete(BOOL b) { m_bCallDelete = b; if( m_Next ) m_Next->SetCallDelete(b); } xType* GetObject() { return(m_ObjData); } CLinked* GetNext() { return(m_cNext); } CLinked* GetPrev() { return(m_cPrev); } CLinked* AddObject(xType* pvPtr,CLinked* prev=NULL); void ReplaseObject(xType* obj) { m_ObjData = obj; } void Reset(); private: xType* m_ObjData; CLinked* m_cPrev; CLinked* m_cNext; BOOL m_bCallDelete; }; template < typename xType > CLinked< xType >::CLinked() { m_bCallDelete = TRUE; m_cPrev = NULL; m_cNext = NULL; m_ObjData = NULL; } template < typename xType > CLinked< xType >::~CLinked() { if( m_bCallDelete ) { if( m_ObjData ) delete( m_ObjData ); m_ObjData = NULL; } if(m_cNext) { delete( m_cNext ); m_cNext = NULL; } } template < typename xType > void CLinked< xType >::Reset() { if( m_bCallDelete ) { if( m_ObjData ) delete( m_ObjData ); m_ObjData = NULL; } if(m_cNext) { delete( m_cNext ); m_cNext = NULL; } m_cPrev = NULL; m_cNext = NULL; m_ObjData = NULL; } template < typename xType > CLinked< xType >* CLinked< xType >::AddObject(xType* pvPtr, CLinked* cpPrev) { // if NULL fail if( pvPtr == NULL ) return( NULL ); // if current has no obj store it if( m_ObjData == NULL ) { m_ObjData = pvPtr; m_cPrev = cpPrev; return( this ); } if( m_cNext ) { return( m_cNext->AddObject(pvPtr,this) ); } m_cNext = new CLinked(); if( m_cNext == NULL ) return(NULL); return( m_cNext->AddObject(pvPtr,this) ); } typedef CLin
You will notice that the error is actually due to Stack Overflow since Reset recursively calls the destructor, which calls the next destructor which... you get the idea. The problem really is that your linked list is incorrect. Your nodes are linked lists? Your CLinked seems to try to be both a node and a linked list. There are other issues as well, but first you should go look at how other people implement linked lists. eg.
template<class T>
class LinkedList
{
public:
class Node
{
public:
Node* GetNext();
Node* GetPrev();
: :private: T element; Node \*pNext; Node \*pPrev;
}
void Reset();
: :
private:
Node *m_pHead;
Node *m_pTail;
int m_nCount;
: :
}-- modified at 23:36 Thursday 8th June, 2006