Thread: Want correction

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Question Want correction

    Hi! I'm programming a simple linked list and I don't know if my ClIterator is correct... I'm actually coding in french but I know that code is code in every language so I hope you will help me even if you don't speak french...

    Here my ClIterateur class... If you have some problem, just post and I'll answer soon.

    Code:
    class ClIterateur
    {
    	friend class ClListe<T>;
    	public :
    	ClIterateur()
    	 : m_pNoeud(0), m_estPremierElementDeLaListe(false)
    	{}
    
    	ClIterateur(TypeNoeud* adresse, bool estPremier)	
    	 : m_pNoeud(adresse), m_estPremierElementDeLaListe(estPremier)
    	{}
    
    	T operator*() const
    	{
    	return m_pNoeud->donnee;
    	}
    
    	T& operator->() const
    	{
    	return &m_pNoeud->donnee; 
    	}
    
    	ClIterateur& operator++()
                	{
    	if (m_estPremierElementDeLaListe)	
                    	{
                    m_estPremierElementDeLaListe= false;
                    	}
    	else
                    {
    	m_pNoeud= m_pNoeud->pSuiv; 
                    }
    	return *this;
    	}
    
    	ClIterateur operator++(int) // i++
    	{
    	ClIterateur ancienIterateur= *this;
    	if (m_estPremierElementDeLaListe)
    		{
    		m_estPremierElementDeLaListe= false;
    		}		
    	else	
    		{
    		m_pNoeud= m_pNoeud->pSuiv;
    		}
    	return ancienIterateur;
    	}
    
    	bool operator==(ClIterateur p_it) const
    	{
    	return (m_pNoeud == p_it.m_pNoeud && m_estPremierElementDeLaListe == p_it.m_estPremierElementDeLaListe); 
    	}
    
    	bool operator!=(ClIterateur p_it) const
    	{
    	return m_pNoeud != p_it.m_pNoeud || m_estPremierElementDeLaListe != p_it.m_estPremierElementDeLaListe; 
    	}
    
    	private :
    		TypeNoeud* m_pNoeud;
    		bool m_estPremierElementDeLaListe;
    		explicit ClIterateur(TypeNoeud* p_pNoeud)
    		 : m_pNoeud(p_pNoeud), m_estPremierElementDeLaListe(false)
    		{}
    };
    What my ClIterateur class is supposed to do : Keep a pointer on a TypeNoeud element (TypeNoeud is a special struct which have a data and a pointer on the next element) and a boolean if it's the first element of the list. But I don't think it is working correctly...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You do have a template line before that class, right?
    Code:
    template <typename T>
    Does it compile?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Yes I have... And It's compiling...

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> But I don't think it is working correctly...

    What made you think this? Posting code that caused problems can help us to help you.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    It's a feeling... My iterator is supposed to keep a pointer on the preceding element, but I know how to do this... Do I have to do a loop in my constructor? Or to add ->pNext after all m_pNoeud ?

    (As you can see, I'm confusing myself...)

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Ok... I have a question. Maybe I find what to do, but I'm not sure and I can't compile where I am now so I risk myself...

    I have this structure of header :

    Code:
    template <typename T>
    class ClListe
        {
        private : struct TypeNoeud {...}
    
        public : class ClIterateur {...}
        }
    In ClIterateur, if this one is friend with ClListe, Can I access to, for example, begin(), which is a function of ClListe?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Maybe I find what to do, but I'm not sure and I can't compile where I am now so I risk myself...
    No more excuses! Compile code online! http://dinkumware.com/exam

    In ClIterateur, if this one is friend with ClListe, Can I access to, for example, begin(), which is a function of ClListe?
    If begin() is a static function, you can; if it isn't, you'll need an object to operate on. If begin() is public then you don't need to be a friend to access it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need correction
    By Lissa in forum C Programming
    Replies: 7
    Last Post: 10-24-2008, 03:53 PM
  2. Error Detection and Correction
    By daisy_polly in forum C Programming
    Replies: 5
    Last Post: 02-22-2006, 06:13 PM
  3. needs a correction
    By threahdead in forum C Programming
    Replies: 5
    Last Post: 10-16-2002, 08:58 PM
  4. Encryption/Decryption -- Correction
    By Abdi in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2002, 08:00 AM
  5. pls help with the correction...
    By leena_teoh in forum C Programming
    Replies: 1
    Last Post: 03-20-2002, 05:18 AM