Dereferencing Class Pointer List

This is a discussion on Dereferencing Class Pointer List within the C++ Programming forums, part of the General Programming Boards category; Hello there! I am currently coding the base of a future program. However, after compiling the code(no errors), there is ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103

    Dereferencing Class Pointer List

    Hello there! I am currently coding the base of a future program. However, after compiling the code(no errors), there is a forced brake point. The Error says: Debug Assertion Failure, Expression: List iterations incompatible.

    Please bare with me, code is not exactly small:

    _medcontainer.h
    Code:
     
    #ifndef _MEDCONTAINER_H
    #define _MEDCONTAINER_H
    
    #include <cstring>
    
    enum TYPE{NAME, DESC, ID, AMOUNT, ALL};
    
    class _medContainer
    {
        public:
        //////////////////////////////////////////////////////////////////////////
        //CONSTRUCTOR(S) AND DESTRUCTOR(S)
            _medContainer();
            _medContainer(char *name, char *desc, char* id, char* amount, TYPE type = NAME);
            virtual ~_medContainer();
        //////////////////////////////////////////////////////////////////////////
        //ACCESSOR FUNCTIONS
    		void SetCounter(unsigned int val)      { m_Counter = val; }
    		void SetName(char* val)				   { m_name = val; }
            void Setdesc(char* val)                { m_desc = val; }
    		void SetId(char* val)				   { m_id = val; }
            void SetAmount(char* val)              { m_amount = val; }
            void SetSortString(char* compareString){compare = compareString;}
    
            char* Getname()     { return m_name; }
            char* Getdesc()     { return m_desc; }
            char* GetCompare()  { return compare; }
            char* Getid()       { return m_id; }
            char* Getamount()   { return m_amount; }
    
            unsigned int GetCounter() { return m_Counter; }
        //////////////////////////////////////////////////////////////////////////
        //OPERATOR OVERLOADING
        _medContainer& operator=(const _medContainer& other);
    
        friend bool operator<(const _medContainer& o1,const _medContainer& o2);
        friend bool operator>(const _medContainer& o1,const _medContainer& o2);
        friend bool operator==(const _medContainer& o1,const _medContainer& o2);
        friend bool operator!=(const _medContainer& o1,const _medContainer& o2);
    
        protected:
        private:
            unsigned int m_Counter; //MEDCONTAINER COUNTER
    		char* m_name;           //NAME OF THE OBJECT
            char* m_desc;           //DESCRIPTION OF PARTICULAR OBJECT
            char* compare;          //STRING THAT WILL BE USED TO COMPRE IN SORT()
    		char* m_id;             //IDENTIFICATION OF OBJECT
            char* m_amount;         //AMOUNT OF PARTICULAR OBJECT
            char m_DlastModified[9];//DAY IN WHICH IT WAS LAST MODIFIED
            char m_TlastModified[9];//TIME IN WHICH IT WAS LAST MODIFIED
    };
    #endif // _MEDCONTAINER_H
    medutility.h
    Code:
    #ifndef MEDUTILITY_H
    #define MEDUTILITY_H
    
    #include "_medContainer.h"          //INCLUDE MEDCONTAINER CLASS
    #include <list>                     //LIST CONTAINERS
    #include <fstream>                  //IN ORDER TO LOAD/SAVE SETTINGS AND LISTS
    
    using namespace std;
    
    typedef list<_medContainer> medList;              //DEFINE A STANDARD LIST KEYWORD
    typedef list<_medContainer*> medSearchResultList; //LIST THAT LINKS TO SEPERATE ELEMENTS OF A LISTS
    typedef _medContainer* _medConPointer;
    
    //////////////////////////////////////////////////////////////////////////
    //STRUCTURE USED TO STORE SEARCH RESULTS
    struct SearchResults
    {
        medSearchResultList Result;
        unsigned int ResultsFound;
    };
    
    //////////////////////////////////////////////////////////////////////////
    //GENERIC UTILITY FUNCTIONS
    medList SortByType(medList &UnsortedList, TYPE SortBy); //USED TO SORT A MEDLIST BY TYPE
    
    medSearchResultList* FindString(medList &SearchedList, char *keyword, TYPE FindWhere);
    
    char* ReturnVariableString(_medContainer &Container, TYPE type); 
    
    //////////////////////////////////////////////////////////////////////////
    //SingleList CLASS - FACILITATES LIST MANIPULATION
    class SingleListClass
    {
    public:
        medList *List;
        //////////////////////////////////////////////////////////////////////////
        //CONSTRUCTOR(S) AND DESTRUCTOR(S)
        //IN CASE FOR TEMPORARY CREATION
        SingleListClass();
        //ALLOWS TO CREAT A LIST WHILE DECLARING THE FIRTS ELEMENT
        SingleListClass(char *name, char *desc, char *id, char *amount);
        //DISMISS A SingleList OBJECT
        ~SingleListClass();
    
        //////////////////////////////////////////////////////////////////////////
        //DEDICATED FUNCTIONS
        medList *GetList()
        {
            return List;
        };
    
        bool    Add(char *name, char *desc, char *id, char *amount);
        void    Add(_medContainer& AddElement);
        void    Add();
        medSearchResultList* Find(char *keyword, TYPE type);
        int     MaxSize(){return (int)List->max_size();}
        medList Sort(TYPE Sortby);
    };
    
    #endif
    All the function definitions are taken care of.... When I do this is main()
    Code:
    #include "_medContainer.h"
    #include "medUtility.h"
    #include <iostream>
    
    void ShowAll(medList &ReferenceList);
    SingleListClass *mynewList = new SingleListClass
    ("Test", "Test", "341873", "1");
    medSearchResultList *Results = new medSearchResultList;
    
    int main(int argc, char *argv[])
    {
    	mynewList->Add();
    	cout<<"BEFORE: "<<endl;
    	ShowAll(*mynewList->GetList());
    	medList::iterator *it = &mynewList->GetList()->begin();
    
    	while( *it != mynewList->GetList()->end())
    	{
    		Results->push_back(&(**it));
    		*it++;
    	}
    	system("PAUSE");
    	cout<<"MODIFIED THROUGH POINTER LIST CLASS"<<endl;
    	ShowAll(*mynewList->GetList());
    
    	system("PAUSE");
    	return(0);
    }
    void ShowAll(medList &ReferenceList)
    {
    	medList::iterator it = ReferenceList.begin();
    
    	while(it != ReferenceList.end())
    	{
    		cout<<"NAME: \n\t"<<it->Getname()<<endl;
    		cout<<"DESC: \n\t"<<it->Getdesc()<<endl;
    		cout<<"ID: \n\t"<<it->Getid()<<endl;
    		cout<<"AMOUNT: \n\t"<<it->Getamount()<<endl;
    		it++;
    	}
    }
    It compiles all right...however when I execute it gives me the error previously mentioned at this place in the code:
    Code:
    while( *it != mynewList->GetList()->end())
    	{
    		Results->push_back(&(**it));
    		*it++;
    	}
    I know that I could use the STL string class but I am experimenting for the moment. Other suggestions are welcomed!

    Thanks in advance for the people who dare to help me!
    Be easy on me...only 14

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> *it++
    Don't you mean (*it)++?

    Why so many pointers? Why not regular objects?

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    >> *it++
    Don't you mean (*it)++?
    Currently I haven't practiced pointers that often, and I want to know all aspects of C++. Thanks for your reply! Unfortunately I still receive the same error.
    Attached Images Attached Images  
    Be easy on me...only 14

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Which line is 309?

    Take the iterator in that line and replace it with it's type, then remove each dereference or function call one at a time with the new type it creates until you have everything at its core type. They should then match.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    medList::iterator *it = &mynewList->GetList()->begin();
    
    while( *it != mynewList->GetList()->end())
    {
    	Results->push_back(&(**it));
    	*it++;
    }
    Why on earth are you creating a pointer to an iterator?

    Also, I'm very tired and there's so many pointers, but should this:
    Code:
    Results->push_back(&(**it));
    be this:
    Code:
    Results->push_back( **it );

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Thanks for your replies! Daved, I cannot add the type at line 309 because it is from <list.h>, I mess with that I mess with the entire library, anyhow VC++ 2005 locks the file from being modified.

    Also, I'm very tired and there's so many pointers, but should this:
    Code:
    Results->push_back(&(**it));
    be this:
    Code:
    Results->push_back( **it );
    It does not compile.
    Be easy on me...only 14

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I don't want you to add the type and compile it. I want you to do it in your head or on paper or in a scratch text file to understand what is happening.

    Also, it would help to know which line the problem is exactly. We'll try this line:
    Code:
    while( *it != mynewList->GetList()->end())
    mynewList->GetList() returns a medList * and calling end() on that will return a list<_medContainer>::iterator (ignore const for the moment). So the expressions is:
    Code:
    *it != list<_medContainer>::iterator
    Now, it is a medList::iterator * and so changing out medList for list<_medContainer> you get:
    Code:
    *(list<_medContainer>::iterator *) != list<_medContainer>::iterator
    You're dereferencing a pointer, so the list<_medContainer>::iterator * becomes just a list<_medContainer>::iterator giving you:
    Code:
    list<_medContainer>::iterator != list<_medContainer>::iterator
    You're comparing two objects of type list<_medContainer>::iterator, so you're fine. That line is probably not the problem. If you understand what I just did, do it for the Results->push_back(&(**it)); code and see what type you are sending to push_back.

  8. #8
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Thanks for the reply! Well here are my thoughts:
    Code:
    while( *it != mynewList->GetList()->end())
    	{					//EXPLANATION(S)
    		Results->push_back(&**it);      //*it = iterator which points to a Container
    					        //**it = (double dereference) Access the pointer that it pointer is pointing to
    						//&(**it) = the address of the object being pointed by two pointers
    
    		/*
    		//APPLICATION
    		Results -> push_back( --- takes a _medContainer* )
    		Therefore Results -> push_back( &(**it) ); is Plausible, isn't it?
    		*/
    					//EXPLANATION(S)
    					//Results->push_back( of type _medContainer* )
    					//(**it) = The object the iterator is pointing to, Hence &_medContainer
    					//So why the need for the extra ampersand(&)? 
    					//Perhaps to forward the Address of the object through the pointer?
    		*it++;
    	}
    however this
    Code:
    while( *it != mynewList->GetList()->end())
    appears to be the problem because once I filter out the rest of while loop(the pushback() and the *it++; ) it still crashes on me

    Anyone got ideas?
    Last edited by toonlover; 08-15-2008 at 03:07 AM.
    Be easy on me...only 14

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> however this appears to be the problem because once I filter out the rest of while loop(the pushback() and the *it++; ) it still crashes on me <<

    Which line is line 309? I still haven't seen the answer to that.

    >> *it++;
    Please change *it++; to (*it)++. It's not correct the other way.

    Now, perhaps use your debugger. Put a breakpoint at the start of the loop and see how far it goes before getting the error (using F10 to step over each line of code one at a time). Does it happen the first time through or does it happen after a couple runs of the loop? Which line exactly does it happen on?

  10. #10
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Well this is the section of code located on list.h that apparently is making my code break:
    Code:
    304: #if _HAS_ITERATOR_DEBUGGING
    305: 		void _Compat(const _Myt_iter& _Right) const
    306: 			{	// test for compatible iterator pair
    307: 			if (this->_Mycont == 0 || this->_Mycont != _Right._Mycont)
    308: 				{
    309: 				_DEBUG_ERROR("list iterators incompatible");
    310: 				_SCL_SECURE_TRAITS_INVALID_ARGUMENT;
    311: 				}
    312: 			}
    313:  #else /* _HAS_ITERATOR_DEBUGGING */
    314: 	protected:
    315:  #endif /* _HAS_ITERATOR_DEBUGGING */
    316:		_Nodeptr _Ptr;	// pointer to node
    317: 		};
    it also breaks before entering the while loop for the first time
    Be easy on me...only 14

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Oh, ok. I didn't realize that line was in another file.

    So if you look in the call stack and double click on the highest line from your own code, where is it?

  12. #12
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    This is what I got for Call Stack:
    Code:
    >	NewMedicine.exe!main(int argc=1, char * * argv=0x00365ea8)  Line 19	C++
     	NewMedicine.exe!__tmainCRTStartup()  Line 586 + 0x19 bytes	C
     	NewMedicine.exe!mainCRTStartup()  Line 403	C
    and the yellow debugging error is on this line of code:
    Code:
    --->     while( *it != mynewList->GetList()->end())
    Be easy on me...only 14

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    And did you use the debugger to identify if it is happening the first time through or after several iterations?

    Did you switch permanently to (*it)++? I ask because using *it++ could lead to the kind of error you are getting by incrementing the pointer instead of the iterator and so the compiler thinks you are still pointing at an iterator but in actuality you aren't. Then when you compare this non-iterator to a real iterator the _Compat function realizes that you messed up.

  14. #14
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Yes, I did check the debugger and it breaks before entering the first iteration. I have fixed the *it++ but the error still prevails. I am getting a headache here....
    Be easy on me...only 14

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Ok, so the following code in main should give the same error:
    Code:
    int main(int argc, char *argv[])
    {
    	mynewList->Add();
    	if (mynewList->GetList()->begin() != mynewList->GetList()->end())
    		cout << "Not equal";
    	else
    		cout << "Equal";
    }
    Right?

    What do the SingleListClass constructor and Add() function definitions look like? Are they properly creating the list member?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-22-2009, 05:03 AM
  2. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  3. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21