Trying to update some legacy code

Hello, I have recently decided to try and get an old project dusted off and see if I can update it to a newer compiler and C++ version. The project will compile without issues in VC 6.0 but I'm trying to get it working with C++ 11 or 14. I've come across an issue with a template I don't understand. I get no errors until I attempt to compile, then the following code throws errors on line 89 and 223.
"syntax error: identifier 'CGenMapAssoc'"


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
#ifndef __DLMAP_H
#define __DLMAP_H

#define DLMAP_DEFAULTSIZE 1009
typedef void* MAPPOSITION;

template<class TKey, class TValue, class TKeyArg>
class TGenMap {
  struct CGenMapAssoc {
	CGenMapAssoc* pNext;
	dword	      HashValue;
	TKey	      Key;
	TValue        Value;
   };

protected:
  CGenMapAssoc** m_ppHashTable;
  dword		 m_RecordCount;
  dword		 m_HashTableSize;
  bool		 m_DeleteValues;

protected:
  virtual CGenMapAssoc* GetAssocNode (TKeyArg Key, dword& Hash);
  virtual CGenMapAssoc* NewAssocNode (void);

public:
  TGenMap();
  virtual ~TGenMap() { Destroy(); }
  virtual void Destroy (void);
  virtual void Delete (TKeyArg Key);
  virtual TValue GetFirstRecord (MAPPOSITION& Position);
  virtual TValue GetNextRecord  (MAPPOSITION& Position);
  virtual TValue GetFirstRecordKey (TKeyArg Key, MAPPOSITION& Position);
  virtual TValue GetNextRecordKey  (TKeyArg Key, MAPPOSITION& Position);
  virtual dword GetNumRecords (void) { return (m_RecordCount); }
  virtual void InitHashTable (const dword Size);
  virtual bool Lookup (TKeyArg Key, TValue& Record);
  virtual void RemoveAll (void);
  virtual void SetAt (TKeyArg Key, TValue& Record);
};

template<class TKey, class TValue, class TKeyArg>
class TGenRefMap : public TGenMap<TKey, TValue, TKeyArg> {
public:
  TGenRefMap() { m_DeleteValues = false; }
};

template<class TKey, class TValue, class TKeyArg>
TGenMap<TKey, TValue, TKeyArg>::TGenMap () {
  m_ppHashTable   = NULL;
  m_RecordCount   = 0;
  m_HashTableSize = DLMAP_DEFAULTSIZE;
  m_DeleteValues  = true;
}

template<class TKey, class TValue, class TKeyArg>
inline void TGenMap<TKey, TValue, TKeyArg>::Destroy (void) {
  RemoveAll();
 }

template<class TKey, class TValue, class TKeyArg>
void TGenMap<TKey, TValue, TKeyArg>::Delete (TKeyArg Key) {
  CGenMapAssoc* pAssoc;
  CGenMapAssoc* pLastAssoc = NULL;
  dword         Hash;

  Hash = HashKey(Key) % m_HashTableSize;
  if (m_ppHashTable == NULL) return;
  
  for (pAssoc = m_ppHashTable[Hash]; pAssoc != NULL; pAssoc = pAssoc->pNext) {

    if (CompareKeys(pAssoc->Key, Key)) {

      if (pLastAssoc != NULL) 
        pLastAssoc->pNext = pAssoc->pNext;
      else
        m_ppHashTable[Hash] = pAssoc->pNext;

      if (m_DeleteValues) { DestroyPointer(pAssoc->Value); }
      DestroyPointer(pAssoc);
      return;
     }

    pLastAssoc = pAssoc;
   }
 }

template<class TKey, class TValue, class TKeyArg>
TGenMap<TKey, TValue, TKeyArg>::CGenMapAssoc* TGenMap<TKey, TValue, TKeyArg>::GetAssocNode (TKeyArg Key, dword& Hash) { // ERROR ON COMPILE
  CGenMapAssoc* pAssoc;

  Hash = HashKey(Key) % m_HashTableSize;
  if (m_ppHashTable == NULL) return (NULL);
  
  for (pAssoc = m_ppHashTable[Hash]; pAssoc != NULL; pAssoc = pAssoc->pNext) {
    if (CompareKeys(pAssoc->Key, Key)) return pAssoc;
   }

  return (NULL);
 }

template<class TKey, class TValue, class TKeyArg>
TValue TGenMap<TKey, TValue, TKeyArg>::GetFirstRecord (MAPPOSITION& Position) {
  CGenMapAssoc* pAssoc;
  dword	       Index;

  Position = (MAPPOSITION) NULL;
  if (m_ppHashTable == NULL) return (NULL);

  for (Index = 0; Index < m_HashTableSize; ++Index) {
    pAssoc = m_ppHashTable[Index];
    
    if (pAssoc != NULL) {
      Position = (MAPPOSITION) pAssoc;
      return (pAssoc->Value);
     }
   }
  return (NULL);
 }

template<class TKey, class TValue, class TKeyArg>
TValue TGenMap<TKey, TValue, TKeyArg>::GetNextRecord (MAPPOSITION& Position) {
  CGenMapAssoc* pAssoc;
  dword	       Index;

  pAssoc = (CGenMapAssoc *) Position;
  if (m_ppHashTable == NULL) return (NULL);
  if (pAssoc        == NULL) return (NULL);

  pAssoc = pAssoc->pNext;

  if (pAssoc != NULL) {
    Position = (MAPPOSITION) pAssoc;
    return (pAssoc->Value);
   }

  for (Index = pAssoc->HashValue + 1; Index < m_HashTableSize; ++Index) {
    pAssoc = m_ppHashTable[Index]; 
    
    if (pAssoc != NULL) {
      Position = (MAPPOSITION) pAssoc;
      return (pAssoc->Value);
     }
   }

  Position = (MAPPOSITION) NULL;
  return (NULL);
 }

template<class TKey, class TValue, class TKeyArg>
TValue TGenMap<TKey, TValue, TKeyArg>::GetFirstRecordKey (TKeyArg Key, MAPPOSITION& Position) {
  CGenMapAssoc* pAssoc;
  dword	        Hash;

  Position = (MAPPOSITION) NULL;
  if (m_ppHashTable == NULL) return (NULL);

  Hash     = HashKey(Key) % m_HashTableSize;
  pAssoc   = m_ppHashTable[Hash];

  while (pAssoc) {
    Position = (MAPPOSITION) pAssoc;
    if (CompareKeys(pAssoc->Key, Key)) return (pAssoc->Value);
    pAssoc = pAssoc->pNext;
  }

  Position = NULL;
  return (NULL);
 }

template<class TKey, class TValue, class TKeyArg>
TValue TGenMap<TKey, TValue, TKeyArg>::GetNextRecordKey (TKeyArg Key, MAPPOSITION& Position) {
  CGenMapAssoc* pAssoc;

  pAssoc = (CGenMapAssoc *) Position;
  if (m_ppHashTable == NULL) return (NULL);
  if (pAssoc        == NULL) return (NULL);

  pAssoc = pAssoc->pNext;

  while (pAssoc) {
    Position = (MAPPOSITION) pAssoc;
    if (CompareKeys(pAssoc->Key, Key)) return (pAssoc->Value);
    pAssoc = pAssoc->pNext;
  }

  Position = NULL;
  return (NULL);
 }

template<class TKey, class TValue, class TKeyArg>
void TGenMap<TKey, TValue, TKeyArg>::InitHashTable (const dword Size) {
  DEFINE_FUNCTION("TGenMap::InitHashTable()");
  
	/* Clear the current table if any */
  DestroyArrayPointer(m_ppHashTable);

	/* Allocate the new hash table */ 
  m_HashTableSize = Size;
  m_ppHashTable   = new CGenMapAssoc* [m_HashTableSize];
  ValidateNewPointer(m_ppHashTable);
  m_RecordCount   = 0;
  memset(m_ppHashTable, 0, sizeof(CGenMapAssoc*) * m_HashTableSize);
 }

template<class TKey, class TValue, class TKeyArg>
bool TGenMap<TKey, TValue, TKeyArg>::Lookup (TKeyArg Key, TValue& Record) {
  CGenMapAssoc* pAssoc;
  dword        Hash;

  pAssoc = GetAssocNode(Key, Hash);

  if (pAssoc == NULL) {
    Record = NULL;
    return (false);
   }

  Record = pAssoc->Value;
  return (true);
 }

template<class TKey, class TValue, class TKeyArg>
TGenMap<TKey, TValue, TKeyArg>::CGenMapAssoc* TGenMap<TKey, TValue, TKeyArg>::NewAssocNode (void) { // ERROR ON COMPILE
  DEFINE_FUNCTION("TGenMap::NewAssocNode()");
  TGenMap::CGenMapAssoc* pAssoc;

  CreatePointer(pAssoc, CGenMapAssoc);
  return (pAssoc);
 }

template<class TKey, class TValue, class TKeyArg>
void TGenMap<TKey, TValue, TKeyArg>::RemoveAll (void) {
  DEFINE_FUNCTION("TGenMap::RemoveAll()");
  CGenMapAssoc*	pAssoc;
  CGenMapAssoc*	pAssoc1;
  dword		Index;

  if (m_ppHashTable != NULL) {
    for (Index = 0; Index < m_HashTableSize; ++Index) {
      for (pAssoc = m_ppHashTable[Index]; pAssoc != NULL; ) {
        pAssoc1 = pAssoc->pNext;

	if (m_DeleteValues) { DestroyPointer(pAssoc->Value); }
        DestroyPointer(pAssoc);

        pAssoc = pAssoc1;
       }
     }

    DestroyArrayPointer(m_ppHashTable);
    m_ppHashTable = NULL;
   }

  m_RecordCount = 0;
 }

template<class TKey, class TValue, class TKeyArg>
void TGenMap<TKey, TValue, TKeyArg>::SetAt (TKeyArg Key, TValue& Record) {
  CGenMapAssoc* pAssoc;
  dword         Hash;

  pAssoc = GetAssocNode(Key, Hash);

  if (pAssoc == NULL) {
    if (m_ppHashTable == NULL) InitHashTable(m_HashTableSize);

    pAssoc = NewAssocNode();
    pAssoc->HashValue = Hash;
    pAssoc->Key       = Key;

    pAssoc->pNext       = m_ppHashTable[Hash];
    m_ppHashTable[Hash] = pAssoc;
    ++m_RecordCount;
   }

  pAssoc->Value = Record;
 }

inline bool CompareKeys (dword Key1, dword Key2) {
  return (Key1 == Key2);
}

inline bool CompareKeys (const char* Key1, const char* Key2) {
  return (StringCompare(Key1, Key2, false) == 0);
}

inline dword HashKey (dword Key) {
  return (Key >> 4);
}

inline dword HashKey (const char* Key) {
  dword nHash = 0;

  while (*Key) {
    nHash = (nHash << 5) + nHash + tolower(*Key);
    ++Key;
   }

  return nHash;
}

#endif 


Sorry for the massive code block.
Last edited on
I am attempting to simplify your code a bit because I don't feel like plugging in all that. I am hoping that my excerpt is still the root cause of your problem.

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
// Example program

using dword = unsigned int;

template<class TKey, class TValue, class TKeyArg>
class TGenMap {
    
    struct CGenMapAssoc {
        CGenMapAssoc* pNext;
        dword HashValue;
        TKey Key;
        TValue Value;
    };
    
    CGenMapAssoc* GetAssocNode (TKeyArg Key, dword& Hash);
};

template<class TKey, class TValue, class TKeyArg>
TGenMap<TKey, TValue, TKeyArg>::CGenMapAssoc*
TGenMap<TKey, TValue, TKeyArg>::GetAssocNode (TKeyArg Key, dword& Hash) { // ERROR ON COMPILE

    return nullptr;

}

int main() { }


The error here is
19:1: error: need 'typename' before 'TGenMap<TKey, TValue, TKeyArg>::CGenMapAssoc' because 'TGenMap<TKey, TValue, TKeyArg>' is a dependent scope


To fix this, add 'typename', like the compiler suggests:
1
2
3
4
5
6
7
template<class TKey, class TValue, class TKeyArg>
typename TGenMap<TKey, TValue, TKeyArg>::CGenMapAssoc*
TGenMap<TKey, TValue, TKeyArg>::GetAssocNode (TKeyArg Key, dword& Hash) { 

    return nullptr; // replace with actual code, of course

}
Last edited on
Thank you for your speedy reply! Adding typename seems to have fixed that issue for me, thank you very much! Got some more errors to fix now ^_^
Topic archived. No new replies allowed.