Hello,
I'm getting only a single compiler error when I try to compile a chunk of code, coming from a template definition, for perspective I will include function definitions on either side of the one that's failing:
template <typename T>
void TypeList<T>::remType(T* t) {
for (int i=0; i<m; i++) {
if (types[i]==t) {
for (map<int,T*>::iterator j=typeInd.begin(); j!=typeInd.end(); j++) {
if (j->second==t) {
typeInd.erase(j);
break;
}
}
delete types[i];
types[i]=NULL;
--c;
}
}
}
template <typename T>
int TypeList<T>::find(T* k) {
for (map<int,T*>::iterator j=typeInd.begin(); j!=typeInd.end(); j++) {
if (j->second==k)
return j->first;
j++;
}
cerr << "Can't find atom type" << endl;
return 0;
}
template <typename T>
void TypeList<T>::remType(int t) {
map<int,T*>::iterator e=typeInd.find(t);
for (int i=0; i<m; i++) {
if (types[i]==e->second) {
delete types[i];
types[i]=NULL;
--c;
break;
}
}
typeInd.erase(e);
}
Now, the fun thing is, for the function in the middle, I get a compiler error expecting a semicolon where the first j is located, and a complaint that the second j is undefined. I do not get this error for the functions on either side of this function definition...the function is appropriately declared in the class definition and I've actually moved it all around in the file to see if it's a bracketing error to no avail.
Any ideas why the compiler is chocking on this?
Also other source files build fine, just the one that calls this function has problems, is that normal?
The type map<int,T*> is called dependent or something like that (forgot exactly). This means that there is a template parameter (T in this case), which determines exactly which type of map will be used. If for example you had used map<int,char*> the situation would be different, because all template arguments for map are now independent from the arguments to the outer template of say, remType. But because you have used the unknown T when instantiating map, the compiler can not be certain which map will result from this. Particularly, there may be template specializations of map, or map may internally use some generic classes that have template specializations, so that map<int,T*>::iterator is say, type in some, static member variable in others, static member function in third.
In order to say to the compiler that iterator is not member object (no matter what kind of member object), but instead is type, you have to use the syntax typename map<int,T*>::iterator everywhere in place of simply map<int,T*>::iterator. Also, the TypeList<T>::remType(T* t) method is not generated until it is called. This is the reason for the error appearing only in the function that uses this method. The error text in those situations is usually nonsensical, so don't worry about that.
(I am not very knowledgeable on the matter myself. But this is the best explanation I have for the moment.)