에러를 못잡겠습니다.
글쓴이: nayana / 작성시간: 일, 2004/10/03 - 1:45오후
Array.h
1 #ifndef ARRAY_H 2 #define ARRAY_H 3 4 5 template<class Datatype> 6 class Array 7 { 8 public: 9 10 Array( int p_size ) 11 { 12 m_array = new Datatype[p_size]; 13 14 m_size = p_size; 15 } 16 17 18 ~Array() 19 { 20 if( m_array != 0 ) 21 delete[] m_array; 22 23 m_array = 0; 24 } 25 26 27 void Resize( int p_size ) 28 { 29 Datatype* newarray = new Datatype[p_size]; 30 31 if( newarray == 0 ) 32 return; 33 34 int min; 35 if( p_size < m_size ) 36 min = p_size; 37 else 38 min = m_size; 39 40 int index; 41 for( index = 0; index < min; index++ ) 42 newarray[index] = m_array[index]; 43 44 m_size = p_size; 45 46 if( m_array != 0 ) 47 delete[] m_array; 48 49 m_array = newarray; 50 } 51 52 53 Datatype& operator[] ( int p_index ) 54 { 55 return m_array[p_index]; 56 } 57 58 59 60 void Insert( Datatype p_item, int p_index ) 61 { 62 int index; 63 64 for( index = m_size - 1; index > p_index; index-- ) 65 m_array[index] = m_array[index - 1]; 66 67 m_array[p_index] = p_item; 68 } 69 70 71 void Remove( int p_index ) 72 { 73 int index; 74 75 for( index = p_index + 1; index < m_size; index++ ) 76 m_array[index - 1] = m_array[index]; 77 } 78 79 int Size() 80 { 81 return m_size; 82 } 83 84 operator Datatype* () 85 { 86 return m_array; 87 } 88 89 bool WriteFile( const char* p_filename ) 90 { 91 FILE* outfile = 0; 92 int written = 0; 93 94 outfile = fopen( p_filename, "wb" ); 95 96 if( outfile == 0 ) 97 return false; 98 99 written = fwrite( m_array, sizeof( Datatype ), m_size, outfile ); 100 fclose( outfile ); 101 102 if( written != m_size ) 103 return false; 104 105 return true; 106 } 107 108 bool ReadFile( const char* p_filename ) 109 { 110 FILE* infile = 0; 111 int read = 0; 112 113 infile = fopen( p_filename, "rb" ); 114 115 if( infile == 0 ) 116 return false; 117 118 read = fread( m_array, sizeof( Datatype ), m_size, infile ); 119 fclose( infile ); 120 121 if( read != m_size ) 122 return false; 123 124 return true; 125 } 126 127 Datatype* m_array; 128 129 int m_size; 130 }; 131 132 #endif
DLinkedList.h
1 #ifndef DLINKEDLIST_H 2 #define DLINKEDLIST_H 3 4 template<class Datatype> class DListNode; 5 template<class Datatype> class DLinkedList; 6 template<class Datatype> class DListIterator; 7 8 template<class Datatype> 9 class DListNode 10 { 11 public: 12 13 Datatype m_data; 14 15 DListNode<Datatype>* m_next; 16 17 DListNode<Datatype>* m_previous; 18 19 20 void Delink() 21 { 22 if( m_previous != 0 ) 23 m_previous->m_next = m_next; 24 25 if( m_next != 0 ) 26 m_next->m_previous = m_previous; 27 } 28 29 void InsertAfter( Datatype p_data ) 30 { 31 DListNode<Datatype>* newnode = new DListNode<Datatype>; 32 newnode->m_data = p_data; 33 34 newnode->m_next = m_next; 35 newnode->m_previous = this; 36 37 if( m_next != 0 ) 38 m_next->m_previous = newnode; 39 40 m_next = newnode; 41 } 42 43 void InsertBefore( Datatype p_data ) 44 { 45 DListNode<Datatype>* newnode = new DListNode<Datatype>; 46 newnode->m_data = p_data; 47 48 newnode->m_next = this; 49 newnode->m_previous = m_previous; 50 51 if( m_previous != 0 ) 52 m_previous->m_next = newnode; 53 54 m_previous = newnode; 55 } 56 57 58 }; 59 60 template<class Datatype> 61 class DLinkedList 62 { 63 public: 64 65 DLinkedList() 66 { 67 m_head = 0; 68 m_tail = 0; 69 m_count = 0; 70 } 71 72 73 ~DLinkedList() 74 { 75 DListNode<Datatype>* node = m_head; 76 DListNode<Datatype>* next; 77 78 while( node != 0 ) 79 { 80 next = node->m_next; 81 82 delete node; 83 84 node = next; 85 } 86 } 87 88 void Append( Datatype p_data ) 89 { 90 if( m_head == 0 ) 91 { 92 m_head = m_tail = new DListNode<Datatype>; 93 m_head->m_data = p_data; 94 m_head->m_next = 0; 95 m_head->m_previous = 0; 96 } 97 else 98 { 99 m_tail->InsertAfter( p_data ); 100 m_tail = m_tail->m_next; 101 } 102 m_count++; 103 } 104 105 void Prepend( Datatype p_data ) 106 { 107 if( m_head == 0 ) 108 { 109 m_head = m_tail = new DListNode<Datatype>; 110 m_head->m_data = p_data; 111 m_head->m_next = 0; 112 m_head->m_previous = 0; 113 } 114 else 115 { 116 m_head->InsertBefore( p_data ); 117 m_head = m_head->m_previous; 118 } 119 m_count++; 120 } 121 122 void RemoveHead() 123 { 124 DListNode<Datatype>* node = 0; 125 126 if( m_head != 0 ) 127 { 128 node = m_head->m_next; 129 130 delete m_head; 131 m_head = node; 132 133 if( m_head == 0 ) 134 m_tail = 0; 135 else 136 m_head->m_previous = 0; 137 138 m_count--; 139 } 140 } 141 void RemoveTail() 142 { 143 DListNode<Datatype>* node = 0; 144 145 if( m_tail != 0 ) 146 { 147 node = m_tail->m_previous; 148 delete m_tail; 149 m_tail = node; 150 if( m_tail == 0 ) 151 m_head = 0; 152 else 153 m_tail->m_next = 0; 154 155 m_count--; 156 } 157 } 158 159 160 void InsertAfter( DListIterator<Datatype>& p_iterator, Datatype p_data ) 161 { 162 if( p_iterator.m_node != 0 ) 163 { 164 p_iterator.m_node->InsertAfter( p_data ); 165 166 if( p_iterator.m_node == m_tail ) 167 m_tail = m_tail->m_next; 168 169 m_count++; 170 } 171 else 172 { 173 Append( p_data ); 174 } 175 } 176 void InsertBefore( DListIterator<Datatype>& p_iterator, Datatype p_data ) 177 { 178 if( p_iterator.m_node != 0 ) 179 { 180 p_iterator.m_node->InsertBefore( p_data ); 181 182 if( p_iterator.m_node == m_head ) 183 m_head = m_head->m_previous; 184 185 m_count++; 186 } 187 else 188 { 189 Prepend( p_data ); 190 } 191 } 192 193 void Remove( DListIterator<Datatype>& p_iterator ) 194 { 195 DListNode<Datatype>* node; 196 197 if( p_iterator.m_node == 0 ) 198 return; 199 200 201 node = p_iterator.m_node; 202 203 if( node == m_head ) 204 { 205 m_head = m_head->m_next; 206 } 207 else if( node == m_tail ) 208 { 209 m_tail = m_tail->m_previous; 210 } 211 212 p_iterator.Forth(); 213 214 node->Delink(); 215 delete node; 216 217 if( m_head == 0 ) 218 m_tail = 0; 219 220 m_count--; 221 } 222 223 DListIterator<Datatype> GetIterator() 224 { 225 return DListIterator<Datatype>( this, m_head ); 226 } 227 228 int Size() 229 { 230 return m_count; 231 } 232 233 bool SaveToDisk( char* p_filename ) 234 { 235 FILE* outfile = 0; 236 DListNode<Datatype>* itr = m_head; 237 238 outfile = fopen( p_filename, "wb" ); 239 240 if( outfile == 0 ) 241 return false; 242 243 fwrite( &m_count, sizeof( int ), 1, outfile ); 244 245 while( itr != 0 ) 246 { 247 fwrite( &(itr->m_data), sizeof( Datatype ), 1, outfile ); 248 itr = itr->m_next; 249 } 250 251 fclose( outfile ); 252 253 return true; 254 } 255 256 bool ReadFromDisk( char* p_filename ) 257 { 258 FILE* infile = 0; 259 Datatype buffer; 260 int count = 0; 261 262 infile = fopen( p_filename, "rb" ); 263 264 if( infile == 0 ) 265 return false; 266 267 fread( &count, sizeof( int ), 1, infile ); 268 269 while( count != 0 ) 270 { 271 fread( &buffer, sizeof( Datatype ), 1, infile ); 272 Append( buffer ); 273 count--; 274 } 275 276 fclose( infile ); 277 278 return true; 279 } 280 281 DListNode<Datatype>* m_head; 282 283 DListNode<Datatype>* m_tail; 284 285 int m_count; 286 }; 287 288 template<class Datatype> 289 class DListIterator 290 { 291 public: 292 293 DListIterator( DLinkedList<Datatype>* p_list = 0, 294 DListNode<Datatype>* p_node = 0 ) 295 { 296 m_list = p_list; 297 m_node = p_node; 298 } 299 300 void Start() 301 { 302 if( m_list != 0 ) 303 m_node = m_list->m_head; 304 } 305 void End() 306 { 307 if( m_list != 0 ) 308 m_node = m_list->m_tail; 309 } 310 311 void Forth() 312 { 313 if( m_node != 0 ) 314 m_node = m_node->m_next; 315 } 316 317 void Back() 318 { 319 if( m_node != 0 ) 320 m_node = m_node->m_previous; 321 } 322 323 Datatype& Item() 324 { 325 return m_node->m_data; 326 } 327 328 bool Valid() 329 { 330 return (m_node != 0); 331 } 332 333 bool operator==( DListIterator<Datatype>& p_rhs ) 334 { 335 if( m_node == p_rhs.m_node && m_list == p_rhs.m_list ) 336 { 337 return true; 338 } 339 return false; 340 } 341 342 343 DListNode<Datatype>* m_node; 344 345 DLinkedList<Datatype>* m_list; 346 }; 347 348 #endif
HashTable.h
1 #ifndef HASHTABLE_H 2 #define HASHTABLE_H 3 4 #include "DLinkedList.h" 5 #include "Array.h" 6 7 template< class Keytype, class Datatype > class HashEntry; 8 template< class Keytype, class Datatype > class HashTable; 9 10 11 template< class Keytype, class Datatype > 12 class HashEntry 13 { 14 public : 15 Keytype m_key; 16 Datatype m_data; 17 }; 18 19 20 template< class Keytype, class Datatype > 21 class HashTable 22 { 23 public : 24 typedef HashEntry< Keytype, Datatype > Entry; 25 26 HashTable( int p_size, unsigned int ( *p_hash )( Keytype ) ); 27 28 void Insert( Keytype p_key, Datatype p_data ); 29 Entry* Find( Keytype p_key ); 30 bool Remove( Keytype p_key ); 31 32 Array< DLinkedList < Entry > > m_table; 33 int m_size; 34 int m_count; 35 36 unsigned long int ( *m_hash )( Keytype ); 37 }; 38 39 //---------------------------------------------------------------------------------------------- 40 template< class Keytype, class Datatype > 41 HashTable< Keytype, Datatype >::HashTable( int p_size, unsigned int ( *p_hash )( Keytype ) ) 42 : m_table( p_size ) 43 //---------------------------------------------------------------------------------------------- 44 { 45 m_size = p_size; 46 m_hash = p_hash; 47 m_count = 0; 48 } 49 50 //---------------------------------------------------------------------------------------------- 51 template< class Keytype, class Datatype > 52 void HashTable< Keytype, Datatype >::Insert( Keytype p_key, Datatype p_data ) 53 //---------------------------------------------------------------------------------------------- 54 { 55 Entry entry; 56 57 entry.m_data = p_data; 58 entry.m_key = p_key; 59 int index = m_hash( p_key ) % m_size; 60 61 m_table[ index ].Append( entry ); 62 m_count++; 63 } 64 65 //---------------------------------------------------------------------------------------------- 66 template< class Keytype, class Datatype > 67 Entry* HashTable< Keytype, Datatype >::Find( Keytype p_key ) 68 //---------------------------------------------------------------------------------------------- 69 { 70 int index = m_hash( p_key ) % m_size; 71 DLinkedList< Entry > itr = m_table[ index ].GetIterator(); 72 73 while( itr.Valid() ) 74 { 75 if( itr.Item().m_key == p_key ) 76 return &( itr.Item() ); 77 itr.Forth(); 78 } 79 80 return 0; 81 } 82 83 //---------------------------------------------------------------------------------------------- 84 template< class Keytype, class Datatype > 85 bool HashTable< Keytype, Datatype >::Remove( Keytype p_key ) 86 //---------------------------------------------------------------------------------------------- 87 { 88 int index = m_hash( p_key ) % m_size; 89 DLinkedList< Entry > itr = m_table[ index ].GetIterator(); 90 while( itr.Valid() ) 91 { 92 if( itr.Item().m_key == p_key ) 93 { 94 m_table[ index ].Remove( itr ); 95 m_count--; 96 return true; 97 } 98 itr.Forth(); 99 } 100 101 return false; 102 } 103 104 #endif
main.cpp
1 #include <cstdio> 2 #include "HashTable.h" 3 4 unsigned long int Hash( int k ) 5 { 6 return k; 7 } 8 9 10 int main( void ) 11 { 12 13 14 return 0; 15 }
에러는 다음과 같습니다.
In file included from main.cpp:2: HashTable.h:67: syntax error before `*' token HashTable.h:67: `Keytype' was not declared in this scope HashTable.h:67: `Datatype' was not declared in this scope HashTable.h:67: template argument 1 is invalid HashTable.h:67: template argument 2 is invalid HashTable.h:67: `Keytype' was not declared in this scope HashTable.h:67: parse error before `)' token HashTable.h:69: ISO C++ forbids declaration of `Find' with no type HashTable.h: In function `int* Find(...)': HashTable.h:70: `p_key' undeclared (first use this function) HashTable.h:70: (Each undeclared identifier is reported only once for each function it appears in.) HashTable.h:70: `m_hash' undeclared (first use this function) HashTable.h:70: `m_size' undeclared (first use this function) HashTable.h:71: `Entry' undeclared (first use this function) HashTable.h:71: template argument 1 is invalid HashTable.h:71: ISO C++ forbids declaration of `itr' with no type HashTable.h:71: `m_table' undeclared (first use this function) HashTable.h:73: request for member `Valid' in `itr', which is of non-aggregate type `int' HashTable.h:75: request for member `Item' in `itr', which is of non-aggregate type `int' HashTable.h:76: request for member `Item' in `itr', which is of non-aggregate type `int' HashTable.h:77: request for member `Forth' in `itr', which is of non-aggregate type `int'
에러를 보니까...Entry가 typedef 먹질 않았습니다. 그래서 HashTable.h에서
67 라인 HashEntry< Keytype, Datatype > * HashTable< Keytype, Datatype >::Find( Keytype p_key )
이렇게 바꾸니까...양호하게 돌아갑니다.
제가 typedef잘못쓴것 같은데....어디가 문제 인지 모르겠습니다.
Forums:
HashTable<Keytype, Datatype>::Entr
HashTable<Keytype, Datatype>::Entry 라고 해야하지 않을까요?
답변에 감사드립니다.제가 개인적으로 판단할때에는...아닌것 같습니다.
답변에 감사드립니다.
제가 개인적으로 판단할때에는...아닌것 같습니다.
리턴값이 Entry* 인데...그렇게하는것은 문법적으로도 어긋나는
것 같습니다.
class 내부에서 선언한 typedef를 외부에서 그냥 접근할 수는 없
class 내부에서 선언한 typedef를 외부에서 그냥 접근할 수는 없습니다.
게으름은 이제 그만
답변에 감사드립니다.님의 말씀대로 했더니...다음과 같은 에러가
답변에 감사드립니다.
님의 말씀대로 했더니...
다음과 같은 에러가 나옵니다.
g++ 3.4 vc 7.1에서 lyster님 말씀대로 하시면 되는군요.
g++ 3.4 vc 7.1에서 lyster님 말씀대로 하시면 되는군요.
Life rushes on, we are distracted
[quote="nayana"]답변에 감사드립니다.님의 말씀대로 했더니
게으름은 이제 그만
댓글 달기