IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Liste doublement chainée générique, erreurs !


Sujet :

C++

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 21
    Points : 17
    Points
    17
    Par défaut Liste doublement chainée générique, erreurs !
    Bonjour tout le monde !
    Voila j'essaye de coder un classe CList qui est en réalité une liste doublement chainée dont les maillons sont des CListEntry, chacun pointant vers l'element suivant et précédant, et contenant un objet de type générique.
    Voici mon code source : (j'ai tout mis dans le hxx mais je compte par la suite faire deux fichiers .hxx, un pour la classe CList et un pour la classe CListEntry)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    ///////////////////////////////////////////////////////////////////
    //                                                               //
    //    FILE......: clistentry.hxx                                 //
    //    VERSION...: 1.0                                            //
    //    DATE......: 01-NOV-2010                                    //
    //    PROGRAM...: any                                            //
    //    AUTHOR....: Virus721                                       //
    //    LICENCE...: LGPL                                           //
    //                                                               //
    ///////////////////////////////////////////////////////////////////
     
    #ifndef __CLISTENTRY_HXX__
    #define __CLISTENTRY_HXX__
     
    namespace nsList
    {
        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
         * CListEntry objects are used as elements of CList objects  *
        \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     
        template <class Generic>
        class CListEntry
        {
          protected :
     
            CListEntry * m_pPrev;
            CListEntry * m_pNext;
            Generic      m_Object;
     
          public :
     
            CListEntry (CListEntry * pPrev  = 0,
                        CListEntry * pNext  = 0,
                        Generic &    object = Generic() )
                : m_pPrev (pPrev), m_pNext (pNext), m_Object (object) {
     
            } // CListEntry (CListENtry*, CListEntry *, Generic &)
     
            inline CListEntry * GetPrev   (void) const { return m_pPrev;  }
            inline CListEntry * GetNext   (void) const { return m_pNext;  }
            inline Generic &    GetObject (void) const { return m_Object; }
     
            inline void SetPrev   (CListEntry * pPrev ) { m_pPrev  = pPrev;  }
            inline void SetNext   (CListEntry * pNext ) { m_pNext  = pNext;  }
            inline void SetObject (Generic &    object) { m_Object = object; }
     
        }; // CListEntry
     
        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
         * CList objects are non-proprietary containers using doubly *
         * linked lists. Elements contained in the list can be       *
         * accessed by using [] operator.                            *
        \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     
        template <class Generic>
        class CList
        {
          protected :
     
            CListEntry<Generic> * m_Head;
     
            static unsigned s_Length;
     
          public :
     
            inline unsigned Length (void) const { return s_Length; }
     
            /* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
             * Creates a new CList with the length given as argument *
             * A list is defined by its Head                         *
            \* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     
            CList (unsigned length = 0)
            {
                CListEntry<Generic> * inserted = m_Head;
                for (unsigned i = 0; i < length; ++i)
                    inserted.InsAfter (inserted);
                s_Length = length;
     
            } // CList (unsigned)
     
            /* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
             * Inserts the CListEntry given as first argument after  *
             * the CListEntry given as second argument in the list   *
            \* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     
            void InsBefore (CListEntry>Generic> * pNewEntry,
                            CListEntry<Generic> * pNext)
            {
                CListEntry<Generic> * pPrev = pNext->GetPrev();
                pNewEntry->SetPrev (pPrev);
                pNewEntry->SetNext (pNext);
                pPrev->SetNext (pNewEntry);
                pNext->SetPrev (pNewEntry);
     
            } // InsBefore (CListEntry *) - OK
     
            /* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
             * Inserts the CListEntry given as first argument after  *
             * the CListEntry given as second argument in the list   *
            \* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     
            void InsAfter (CListEntry<Generic> * pNewEntry,
                           CListEntry<Generic> * pPrev)
            {
                CListEntry<Generic> * pNext = pPrev->GetNext();
                pNewEntry->SetPrev (pPrev);
                pNewEntry->SetNext (pNext);
                pPrev->SetNext (pNewEntry);
                pNext->SetPrev (pNewEntry);
     
            } // InsAfter (CListEntry *) - OK
     
            /* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
             * Returns the object contained by the CListEntry in     *
             * the "this" list at the rank given as argument. This   *
             * operator has to be used with parsimony, especially    *
             * when using very long lists.                           *
            \* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     
            Generic & operator [] (unsigned rank)
            {
                CListEntry<Generic> * entry = m_Head;
                for (unsigned i = 0; i < rank - 1; ++i)
                    entry = entry->GetNext();
                return entry->GetObject();
     
            } // operator [] (unsigned)
     
        }; // CList
     
    } // nsList
     
    /* TEST */
    #include <iostream>
     
    int main (void)
    {
        unsigned listSize;
        std::cout << "Enter list size : " << std::endl;
        std::cin >> listSize;
     
        nsList::CList<unsigned> list (listSize);
     
        for (unsigned i = 0; i < list.Length(); ++i)
        {
            unsigned elem = (list[i] = i);
            std::cout << elem << std::endl;
        }
     
        return 0;
     
    } // main()
    /* /TEST */
     
    #endif
     
    /* clistentry.h */
    Et je me retrouve avec les erreurs de compilation suivantes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    virus721@area721m:~/Devel/workspace/linked_list$ g++ clistentry.hxx
    clistentry.hxx:87: error: ‘CListEntry’ is not a type
    clistentry.hxx:87: error: expected ‘,’ or ‘...’ before ‘>’ token
    clistentry.hxx: In member function ‘void nsList::CList<Generic>::InsBefore(int)’:
    clistentry.hxx:90: error: ‘pNext’ was not declared in this scope
    clistentry.hxx:91: error: ‘pNewEntry’ was not declared in this scope
    clistentry.hxx: In constructor ‘nsList::CList<Generic>::CList(unsigned int) [with Generic = unsigned int]’:
    clistentry.hxx:143:   instantiated from here
    clistentry.hxx:77: error: request for member ‘InsAfter’ in ‘inserted’, which is of non-class type ‘nsList::CListEntry<unsigned int>*’
    clistentry.hxx: In member function ‘Generic& nsList::CListEntry<Generic>::GetObject() const [with Generic = unsigned int]’:
    clistentry.hxx:126:   instantiated from ‘Generic& nsList::CList<Generic>::operator[](unsigned int) [with Generic = unsigned int]’
    clistentry.hxx:147:   instantiated from here
    clistentry.hxx:41: error: invalid initialization of reference of type ‘unsigned int&’ from expression of type ‘const unsigned int’
    virus721@area721m:~/Devel/workspace/linked_list$
    Il doit y avoir quelques petites choses que je n'ai pas compris avec la généricité et les références, donc si quelqu'un qui connait bien tout ca avait quelques minutes à me consacrer pour m'expliquer ce qui ne va pas (je corrigerai moi même une fois que j'aurai compris) ca serait très sympa !
    Merci d'avance !

  2. #2
    En attente de confirmation mail
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Octobre 2010
    Messages
    501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Octobre 2010
    Messages : 501
    Points : 1 060
    Points
    1 060
    Par défaut
    Bonsoir,

    Ligne 87, comme c'est marqué dans le log de compilation:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    void InsBefore (CListEntry>Generic> * pNewEntry,
    devrait être

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    void InsBefore (CListEntry<Generic> * pNewEntry,
    Le ">" devrait être "<"

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 21
    Points : 17
    Points
    17
    Par défaut
    re !
    Merci beaucoup pour ton aide ! J'ai aussi trouvé certaines autres erreurs ( . au lieu de -> avec un pointeur et une fonction membre que j'avais faite passer de la classe CListEntry à la classe CList sans vérifier tout le code).
    Mais qu'en est-t-il des autres erreurs ? Il semble que j'ai fait deux fois la même donc c'est peut être pas juste de la syntaxe ! Si quelqu'un sait d'ou vient le problème...
    Merci d'avance !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    virus721@area721m:~/Devel/workspace/list$ g++ clistentry.hxx
    clistentry.hxx: In member function ‘Generic& nsList::CListEntry<Generic>::GetObject() const [with Generic = unsigned int]’:
    clistentry.hxx:126:   instantiated from ‘Generic& nsList::CList<Generic>::operator[](unsigned int) [with Generic = unsigned int]’
    clistentry.hxx:147:   instantiated from here
    clistentry.hxx:41: error: invalid initialization of reference of type ‘unsigned int&’ from expression of type ‘const unsigned int’
    virus721@area721m:~/Devel/workspace/list$

  4. #4
    screetch
    Invité(e)
    Par défaut
    ta methode GetObject est const et tu retournes une référence non const sur l'objet interne ce qui est interdit

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 21
    Points : 17
    Points
    17
    Par défaut
    Merci ca m'a corrigé une des erreurs et j'ai pu arranger le reste !
    A la prochaine ! :p

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 10
    Dernier message: 16/11/2010, 09h26
  2. Réponses: 2
    Dernier message: 24/03/2007, 12h48
  3. Problème sur les listes doublement chainée
    Par Traouspont dans le forum C
    Réponses: 5
    Dernier message: 05/01/2007, 12h02
  4. Pb Liste doublement chainée template
    Par ederf dans le forum Langage
    Réponses: 5
    Dernier message: 19/11/2006, 10h35
  5. Liste doublement chainée
    Par sorry60 dans le forum C
    Réponses: 23
    Dernier message: 03/12/2005, 17h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo