Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Templates Help?

Templates Help?

Scheduled Pinned Locked Moved C / C++ / MFC
helpwpfdata-structuresdebuggingquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    G_S
    wrote on last edited by
    #1

    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
    
    J 1 Reply Last reply
    0
    • G G_S

      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
      
      J Offline
      J Offline
      Justin Tay
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups