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

SL & STL C++ Discussion :

erreur de compilation


Sujet :

SL & STL C++

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 11
    Par défaut erreur de compilation
    Bonjour à tous

    j'ai un problème de compilation d'un projet dont je dois prendre la suite. Je fais la compilation avec g++, sous linux.

    Toutes les erreurs interviennent dans deux fichiers *.hpp qui contiennent les déclarations et les définitions de classes templates. Il n'y a pas de templates dans le reste du code, donc je pense que ça vient de là.

    g++ -Wno-non-template-friend -O4 -c -o Component.o Component.cpp
    In file included from /usr/include/c++/4.2/backward/iostream.h:31,
    from Component.cpp:3:
    /usr/include/c++/4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
    In file included from Array1D.hpp:19,
    from HashTable.hpp:16,
    from Component.cpp:7:
    Exception.h: In constructor ‘Exception::Exception()’:
    Exception.h:20: warning: deprecated conversion from string constant to ‘char*’
    In file included from HashTable.hpp:18,
    from Component.cpp:7:
    List.hpp: In destructor ‘List<T>::~List()’:
    List.hpp:101: error: ‘head’ was not declared in this scope
    List.hpp: In constructor ‘List<T>::List()’:
    List.hpp:251: error: ‘head’ was not declared in this scope
    List.hpp:251: error: ‘curr’ was not declared in this scope
    List.hpp:251: error: expected type-specifier before ‘Link’
    List.hpp:251: error: expected `;' before ‘Link’
    List.hpp: In copy constructor ‘List<T>::List(const List<T>&)’:
    List.hpp:260: error: ‘head’ was not declared in this scope
    List.hpp:260: error: ‘curr’ was not declared in this scope
    List.hpp:260: error: expected type-specifier before ‘Link’
    List.hpp:260: error: expected `;' before ‘Link’
    List.hpp: In member function ‘List<T>& List<T>::operator+=(const List<T>&)’:
    List.hpp:300: error: there are no arguments to ‘end’ that depend on a template parameter, so a declaration of ‘end’ must be available
    List.hpp:300: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
    Je crois comprendre que les erreurs viennent de mauvaises définitions de certaines variables, mais je vois pas comment les résoudre.
    Est-ce que ca vient du fait que les classes sont définies et déclarées dans le même fichiers? Est ce qu'il me manque des librairies?

  2. #2
    Membre éclairé Avatar de Trunks
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2004
    Messages
    534
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2004
    Messages : 534
    Par défaut
    Remplace les headers C++

    par

    Par exemple

    par

    S'il s'agit d'un header de type C par contre, comme <stdlib.h>, tu peux remplacer par <cstdlib> pour la plupart il me semble.

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 11
    Par défaut
    Bonjour,

    voilà j'ai fait ce que vous m'avez dit. maintenant je me retrouve avec des erreurs du type :

    In file included from Material.h:5,
    from Component.h:7,
    from Component.cpp:10:
    Physical.h:29: error: ISO C++ forbids declaration of ‘ostream’ with no type
    Physical.h:29: error: ‘ostream’ is neither function nor member function; cannot be declared friend
    Physical.h:29: error: expected ‘;’ before ‘&’ token
    Physical.h:49: error: ‘ostream’ has not been declared
    Physical.h:49: error: ‘cout’ was not declared in this scope



    j'ai essayé d'inclure ostream et istream mais rien n'y fait : que veut dire :
    ISO C++ forbids declaration of ‘ostream’ with no type sachant que ma ligne de commande est :


    friend ostream & operator << (ostream &out, Color color);

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 11
    Par défaut template et g++
    Toutes les erreurs interviennent à nouveau dans mes deux fichiers *.hpp qui contiennent les déclarations et les définitions de classes templates. Je ne sais pas si j'ai oublié d'insérer une librairie ou si mes template sont mal déclarés ?

    un exemple de mon code :

    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
     class Link
      {
      public:
     
        Link *nxt;
        Link *prev;
        T     element;
        Link(Link *p = 0, Link *n = 0){
          prev= p;
          nxt = n;
          if (prev)
    	prev->nxt = this;
          if (nxt)
    	nxt->prev = this;
        }
        ~Link(){
          if(prev)
    	prev->nxt = nxt;
          if(nxt)
    	nxt->prev = prev;
        }
      };
     
    protected:
     
      ListIterator(){ head = curr = 0; }
     
      Link *head;
      Link *curr;
     
      friend class List< T >;
      friend class FullList< T >;
     
    };
     
     
    template< class T > class List: public ListIterator< T >
    {
     
    public:
      List();
      List(const List &l);
      ~List(){ 
        flush();
        delete head;
      }
     
      List & operator = (const List &l);
      List & operator = (const ListIterator< T >&li);
      List   operator + (const List &l) const;
      List & operator +=(const List &l);
      T    & operator [](int index);
     
      void insert(const T &el);
      void insert(ListIterator< T > &li, const T &el);
      int  nElements() const;
      void remove();
      void remove(ListIterator< T > &li);
      void flush();
     
    protected:
      int nels;
      T **ptrArray;
     
    };
     
    template< class T > class FullList : public List< T >
    {
    public: 
     
      FullList():List< T >(){}
      FullList(const List< T > &list):List< T >(list){}
     
     
      FullList & operator = (const ListIterator< T > &li);
      int  search(const T &el);
      int  search(ListIterator< T >&, const T &el) const;
      void write(ostream &out = cout) const;
      void read(istream &in = cin);
     
      friend ostream & operator<<(ostream &out, const FullList< T > &l);
      friend istream & operator>>(istream &in, FullList< T > &l);
     
    };
     
     
    template< class T >
    inline T& ListIterator< T >::operator()() const
    {
      if (curr == head)
        throw Exception("No current element!");
      return curr->element;
    }
     
    template< class T >
    inline ListIterator< T >& ListIterator< T >::operator++()
    {
      curr = curr->nxt;
      return *this;
    }
     
    template< class T >
    inline ListIterator< T >& ListIterator< T >::operator--()
    {
      curr = curr->prev;
      return *this;
    }
    et j'obtiens des erreurs comme :

    List.hpp: In member function ‘void List<T>::flush()’:
    List.hpp:401: error: ‘curr’ was not declared in this scope
    List.hpp:401: error: ‘head’ was not declared in this scope
    List.hpp:403: error: ‘Link’ was not declared in this scope
    List.hpp:403: error: ‘tmp’ was not declared in this scope

  5. #5
    r0d
    r0d est déconnecté
    Membre expérimenté

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2004
    Messages
    4 321
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Août 2004
    Messages : 4 321
    Billets dans le blog
    2
    Par défaut
    Ta classe Link est coupée en deux: le }; juste avant protected: est en trop.

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 13
    Par défaut
    Non, elle a oublié le début du code. La classe Link est déclarée dans la déclaration d'une classe ListIterator:

    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
    template< class T > class ListIterator
    {
     
    public:
      ListIterator(const List< T > &list) : 
        head(list.head), 
        curr(list.curr) { }
     
      T            & operator()()const;
      ListIterator & operator++();
      ListIterator & operator--();
      ListIterator & operator=(const ListIterator< T > &li);
      int            operator!() const;
      int            operator==(const ListIterator&) const;
      int            operator!=(const ListIterator&) const;
     
      T  & current() const;
      int  last() const;
      int  header() const;
      T  & previous() const;
      int  first() const;
      int  end();
      T  & next() const;
      int  beginning();
     
      class Link
      {
      public:
     
        Link *nxt;
        Link *prev;
        T     element;
        Link(Link *p = 0, Link *n = 0){
          prev= p;
          nxt = n;
          if (prev)
    	prev->nxt = this;
          if (nxt)
    	nxt->prev = this;
        }
        ~Link(){
          if(prev)
    	prev->nxt = nxt;
          if(nxt)
    	nxt->prev = prev;
        }
      };
     
    protected:
     
      ListIterator(){ head = curr = 0; }
     
      Link *head;
      Link *curr;
     
      friend class List< T >;
      friend class FullList< T >;
     
    };

  7. #7
    r0d
    r0d est déconnecté
    Membre expérimenté

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2004
    Messages
    4 321
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Août 2004
    Messages : 4 321
    Billets dans le blog
    2
    Par défaut
    Ok. Pouvez-vous nous dire quelles sont les lignes qui génèrent des erreurs?

  8. #8
    Membre averti
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 13
    Par défaut
    toutes les lignes des fonctions membres de List qui contiennent 'head' et 'curr' (membres de Link), par exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
      ~List(){ 
        flush();
        delete head;
    le delete head appelle une erreur 'head was not declared in this scope'.

    Toutes les lignes où sont déclarées des instances de Link, par exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    template< class T >
    List< T >::List() : ListIterator< T >(*this)
    {
      head = curr = new Link;
      curr->nxt = curr->prev = head;
      ptrArray = 0;
      nels = 0;
    }
    le 'new Link' va appeller une erreur : 'expected type-specifier before Link'

    il y a quelques autres erreurs, mais elles sont très similairesà ces deux là.


    J'ai essayé de rajouter des this-> avant les variables head et curr pour supprimer les erreurs de scope, mais ca ne résoud pas tout, et je suis pas sûr que ce soit la bonne solution. Pour être tout à fait franc, j'ai pas trop envie de faire des modifications au petit bonheur la chance pour résoudre les erreurs, mais comme c'est pas mon code, je préfèrerais comprendre ce qui se passe.

    Merci

  9. #9
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    Peux-tu reposter complètement ton code. J'ai récupéré les petits bouts que tu as déjà posté, et je n'ai pas d'erreur modulo les définitions qui manquent?

  10. #10
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    Citation Envoyé par anslab Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    template< class T >
    List< T >::List() : ListIterator< T >(*this)
    {
      head = curr = new Link;
      curr->nxt = curr->prev = head;
      ptrArray = 0;
      nels = 0;
    }
    ?????

  11. #11
    Membre averti
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 13
    Par défaut
    @3DArchi : yafiygi :

    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
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
     
    #ifndef List_HPP_
    #define List_HPP_
     
    // Exception class
    #include "Exception.h"
     
     
    template< class T > class List;
     
    template< class T > class FullList;
     
    template< class T > class ListIterator
    {
     
    public:
      ListIterator(const List< T > &list) : 
        head(list.head), 
        curr(list.curr) { }
     
      T            & operator()()const;
      ListIterator & operator++();
      ListIterator & operator--();
      ListIterator & operator=(const ListIterator< T > &li);
      int            operator!() const;
      int            operator==(const ListIterator&) const;
      int            operator!=(const ListIterator&) const;
     
      T  & current() const;
      int  last() const;
      int  header() const;
      T  & previous() const;
      int  first() const;
      int  end();
      T  & next() const;
      int  beginning();
     
      class Link
      {
      public:
     
        Link *nxt;
        Link *prev;
        T     element;
        Link(Link *p = 0, Link *n = 0){
          prev= p;
          nxt = n;
          if (prev)
    	prev->nxt = this;
          if (nxt)
    	nxt->prev = this;
        }
        ~Link(){
          if(prev)
    	prev->nxt = nxt;
          if(nxt)
    	nxt->prev = prev;
        }
      };
     
    protected:
     
      ListIterator(){ head = curr = 0; }
     
      Link *head;
      Link *curr;
     
      friend class List< T >;
      friend class FullList< T >;
     
    };
     
    template< class T > class List: public ListIterator< T >
    {
     
    public:
      List();
      List(const List &l);
      ~List(){ 
        flush();
        delete head;
      }
     
      List & operator = (const List &l);
      List & operator = (const ListIterator< T >&li);
      List   operator + (const List &l) const;
      List & operator +=(const List &l);
      T    & operator [](int index);
     
      void insert(const T &el);
      void insert(ListIterator< T > &li, const T &el);
      int  nElements() const;
      void remove();
      void remove(ListIterator< T > &li);
      void flush();
     
    protected:
      int nels;
      T **ptrArray;
     
    };
     
    template< class T > class FullList : public List< T >
    {
    public: 
     
      FullList():List< T >(){}
      FullList(const List< T > &list):List< T >(list){}
     
     
      FullList & operator = (const ListIterator< T > &li);
      int  search(const T &el);
      int  search(ListIterator< T >&, const T &el) const;
      void write(ostream &out = cout) const;
      void read(istream &in = cin);
     
      friend ostream & operator<<(ostream &out, const FullList< T > &l);
      friend istream & operator>>(istream &in, FullList< T > &l);
     
    };
     
     
    template< class T >
    inline T& ListIterator< T >::operator()() const
    {
      if (curr == head)
        throw Exception("No current element!");
      return curr->element;
    }
     
    template< class T >
    inline ListIterator< T >& ListIterator< T >::operator++()
    {
      curr = curr->nxt;
      return *this;
    }
     
    template< class T >
    inline ListIterator< T >& ListIterator< T >::operator--()
    {
      curr = curr->prev;
      return *this;
    }
     
    template< class T >
    ListIterator< T >& ListIterator< T >::operator =(const ListIterator< T > &li)
    {
      if (head != li.head)
        throw Exception("Invalid iterators!");
      curr = li.curr;
      return *this;
    }
     
    template< class T >
    inline int ListIterator< T >:: operator !() const
    {
      return curr != head;
    }
     
    template< class T >
    int ListIterator< T >::operator ==(const ListIterator< T > &li) const
    {
      return (head == li.head && curr == li.curr);
    }
     
    template< class T >
    int ListIterator< T >::operator !=(const ListIterator< T > &li) const
    {
      return (head != li.head || curr != li.curr);
    }
     
    template< class T >
    inline T& ListIterator< T >::current() const
    {
      if(curr == head)
        throw Exception("No current element!");
      return curr->element;
    }
     
    template< class T >
    inline int ListIterator< T >::last() const
    {
      return (curr != head && curr == head->prev);
    }
     
    template< class T >
    inline int ListIterator< T >::header() const
    {
      return curr == head;
    }
     
    template< class T >
    inline T& ListIterator< T >::previous() const
    {
      if (curr->prev == head)
        throw Exception("No previous element!");
      return curr->prev->element;
    }
     
    template< class T >
    inline int ListIterator< T >::first() const
    {
      return (curr != head && curr == head->nxt);
    }
     
    template< class T >
    inline int ListIterator< T >::end()
    {
      curr = head->prev;
      return curr != head;
    }
     
    template< class T >
    inline T& ListIterator< T >::next() const
    {
      if (curr->nxt == head)
        throw Exception("No next element");
      return curr->nxt->element;
    }
     
    template< class T >
    inline int ListIterator< T >::beginning()
    {
      curr = head->nxt;
      return curr != head;
    }
     
    template< class T >
    List< T >::List() : ListIterator< T >(*this)
    {
      head = curr = new Link;
      curr->nxt = curr->prev = head;
      ptrArray = 0;
      nels = 0;
    }
     
    template< class T >
    List< T >::List(const List< T > &l) : ListIterator< T >(*this)
    {
      head = curr = new Link;
      curr->nxt = curr->prev = head;
      ptrArray = 0;
      nels = 0;
      operator =(l);
    }
     
    template< class T >
    List< T > & List< T >::operator =(const List< T > &l)
    {
      if (&l == this)
        return *this;
      flush();
      ListIterator< T > tmp(l);
      for (tmp.beginning(); !tmp; ++tmp)
        insert(tmp() );
      return *this;
    }
     
    template< class T >
    List< T > & List< T >::operator=(const ListIterator< T > &li)
    {
      ListIterator< T >::operator =(li);
      return *this;
    }
     
    template< class T >
    List< T > List< T >::operator +(const List< T > &right) const
    {
      List< T > res = (*this);
      res.end();
      ListIterator< T > tmp(right);
      for(tmp.beginning(); !tmp; ++tmp)
        res.insert(tmp() );
      return res;
    }
     
    template< class T >
    List< T > & List< T >::operator +=(const List< T > &right)
    {
      end();
      ListIterator< T > tmp(right);
      for(tmp.beginning(); !tmp; ++tmp)
        insert(tmp() );
      return *this;
    }
     
    template< class T >
    inline T & List< T >::operator[](int index)
    {
      if (!ptrArray){
        ptrArray = new T*[nels];
        if (! ptrArray)
          throw Exception("Out of memory!");
        ListIterator< T > tmp (*this);
        int i = 0;
        for (tmp.beginning(); !tmp; ++tmp)
          ptrArray[ i++ ] = &tmp();
      }
      if (index < 0 || index >= nels)
        throw Exception("Invalid array index");
      return *ptrArray[ index ];
    }
     
    template< class T >
    void List< T >::insert(const T &el)
    {
      if(ptrArray){
        delete ptrArray;
        ptrArray = 0;
      }
      curr = new Link(curr, curr->nxt);
      if (curr == 0)
        throw Exception("Out of memory!");
      curr->element = el;
      nels++;
    }
     
    template< class T >
    void List< T >::insert(ListIterator< T > &li, const T &el)
    {
      if (head != li.head)
        throw Exception("Incompatible iterators!");
      if (ptrArray){
        delete ptrArray;
        ptrArray = 0;
      }
      li.curr = new Link(li.curr, li.curr->nxt);
      if (li.curr == 0)
        throw Exception("Out of memory!");
      li.curr->element = el;
      nels++;
    }
     
    template< class T >
    inline int List< T >::nElements() const
    {
      return nels;
    }
     
    template< class T >
    void List< T >::remove()
    {
      if (ptrArray){
        delete ptrArray;
        ptrArray = 0;
      }
      if (curr == head)
        throw Exception("No current element!");
      curr = curr->prev;
      Link *tmp = curr->nxt;
      delete tmp;
      nels--;
    }
     
    template< class T >
    void List< T >::remove(ListIterator< T > &li)
    {
      if( head != li.head)
        throw Exception("Incompatible iterators");
      if (ptrArray){
        delete ptrArray;
        ptrArray = 0;
      }
      if (li.curr == head)
        throw Exception("No current element!");
      if (curr = li.curr)
        curr = li.curr->prev;
      li.curr = li.curr->prev;
      Link *tmp = li.curr->nxt;
      delete tmp;
      nels--;
    }
     
    template< class T >
    void List< T >::flush()
    {
      if (ptrArray){
        delete ptrArray;
        ptrArray = 0;
      }
      curr = head;
      while (curr->nxt != head){
        Link *tmp = curr->nxt;
        delete tmp;
      }
      nels = 0;
    }
     
    template< class T >
    FullList< T > & FullList< T >::operator =(const ListIterator< T > &li)
    {
      ListIterator< T >::operator =(li);
      return *this;
    }
     
    template< class T >
    int FullList< T >::search(const T &el)
    {
      for( ; curr != head; curr = curr->nxt)
        if (el == curr->element)
          break;
      return curr != head;
    }
     
    template< class T >
    int FullList< T >::search(ListIterator< T > &li, const T &el) const
    {
      if (head != li.head)
        throw Exception("Incompatible iterators!");
      for ( ; !li; ++li)
        if (el == li() )
          break;
      return !li;
    }
     
    template< class T >
    void FullList< T >::write(ostream &out) const
    {
      out << '(' << nels << ')' << endl;
      ListIterator< T > li(*this);
      for (li.beginning(); !li; ++li)
        out << li() << endl;
      out << endl;
    }
     
    template< class T >
    void FullList< T >::read(istream &in)
    {
      int n;
      char c1 = ' ', c2 = ' ';
      in >> c1 >> n >> c2;
      if (c1 != '(' || c2 != ')' || n < 0)
        throw Exception("Invalid format!");
      flush();
      T tmp;
      for (int i = 0; i < n; i++){
        if ( !(in >> tmp) )
          throw Exception("Invalid format!");
        insert(tmp);
      }
    }
     
    template< class T >
    ostream & operator <<(ostream &out, const FullList< T > &fl)
    {
      fl.write(out);
      return out;
    }
     
    template< class T >
    istream & operator >>(istream &in, FullList< T > &fl)
    {
      fl.read(in);
      return in;
    }
     
    #endif
    pour le *this, oui c'est curieux, mais pour tout te dire c'est du code que ma copine (DVD-RW) a récupéré et elle me l'a tendu en disant "compile". Je sais juste que ca a près de 10 ans.

  12. #12
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    Citation Envoyé par anslab Voir le message
    pour le *this, oui c'est curieux, mais pour tout te dire c'est du code que ma copine (DVD-RW) a récupéré et elle me l'a tendu en disant "compile". Je sais juste que ca a près de 10 ans.
    [JOKE]C'est pas bien de s'abriter derrière les autres [/JOKE]
    Sérieusement, tu ne peux pas faire *this à ce niveau dans un constructeur car les constructeurs parents n'ayant pas été appelé, tu n'es pas garanti que *this soit correctement initialisé.
    D'ailleurs, c'est assez bancale d'avoir List<T> qui dérive de ListIterator<T> et que le constructeur de ListIterator<T> demande un List<T> en paramètre...

    Sinon, j'ai mis un peu de temps à reproduire ton problème car Visual accepte très bien ton code.
    Voilà la solution, mais il ne faut pas la suivre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    ListIterator< T >::curr = new typename ListIterator< T >::Link(ListIterator< T >::curr, ListIterator< T >::curr->nxt)
    En gros, préfixé par ListIterator<T>:: pour accéder à ce qui est déclarer dans ListIterator.

    Pourquoi ne faut-il pas la suivre??? Parce que tout ce qui est fait dans List avec les membres de ListIterator devrait en fait être de la responsabilité de ListIterator. Donc être fait dans cette classe ! Bref, c'est un gros problème de modélisation.

Discussions similaires

  1. Erreur de compilation après modification du Uses
    Par DevelOpeR13 dans le forum Langage
    Réponses: 5
    Dernier message: 30/10/2007, 15h23
  2. Réponses: 2
    Dernier message: 23/09/2003, 15h32
  3. Réponses: 10
    Dernier message: 22/09/2003, 22h58
  4. Réponses: 4
    Dernier message: 27/08/2003, 22h34
  5. Réponses: 2
    Dernier message: 05/03/2003, 00h24

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