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)
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
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 */
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 !
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$
Merci d'avance !
Partager