Xcode + template pointer vector iterator = error?

Started by
3 comments, last by MaulingMonkey 15 years, 3 months ago

template<class T>
class MyClass
{
public:
     myMethod();
};

template<class T>
void MyClass<T>::myMethod()
{
	vector<string>::iterator it1;
	vector<T*>::iterator it2;  //error happens here

     //More code here
}

Alright, so I'm making this template class from code that I had working in Visual C++ 2005. I copied the code over to Xcode and it seems to be giving me a weird error: error: expected ';' before 'it2' It's nothing so simple as me missing a semi-colon for the class or a function above this one or forgetting to include the right header. It appears to me that the error is actually happening because Xcode is having trouble recognizing a vector of type 'T*' for one reason or another. I only think this because it gives me no error for the first iterator of a vector of strings. I was wondering if this is at all related to Xcode or if there's some stupid mistake I've made somewhere in the syntax.
"Considering a thesis that the general populous of the mentally less fortunate are most likely incapable of being aware of their own neural inadequacies, it could be concluded that my everyday demeanor is moderately educational." - Me
Advertisement
The problem is that vector<T>::iterator is a dependant type, the standard requires that you use the typename keyword to inform the compiler of this. Visual C++ obviously accepts code without this keyword, it is not a weakness in Xcode - which AFAIK uses GCC as its compiler.
template<class T>class MyClass{public:     myMethod();};template<class T>void MyClass<T>::myMethod(){	vector<string>::iterator it1;	typename vector<T*>::iterator it2;     //More code here}

Google the emphasised terms (along with "C++") to get more information for why this is necessary.
TO back up what rip-off said:
vector<string>::iterator it1; // Independent type. (Although, the actual term is, *ahem* non-dependent.)vector<T*>::iterator it2; // Dependent type.

The type being dependent means that the type T is dependent on a template parameter. For all the compiler knows, vector<T*>::iterator is actually a static variable. typename assures it it's not. vector<T*> may or may not be dependent (I don't know) but vector is known from global scope to be a class so there's no issue.

G++ is more strict in correctness here, and most places it seems, about not assuming that ::iterator is a type. I suppose this is to enforce proper, portable code.
I appreciate the swift and thorough response, guys. This is why I need to explore the realm of GNU more often. I understand completely and it fixed up my errors. Thanks again.
"Considering a thesis that the general populous of the mentally less fortunate are most likely incapable of being aware of their own neural inadequacies, it could be concluded that my everyday demeanor is moderately educational." - Me
Quote:vector<T*> may or may not be dependent (I don't know) but vector is known from global scope to be a class so there's no issue.

It's not, for the record.

Quote:G++ is more strict in correctness here, and most places it seems, about not assuming that ::iterator is a type. I suppose this is to enforce proper, portable code.

Yep. I remember the days GCC didn't require it. The last time I'd used it, it didn't care about dependent template-name syntax, though, like MSVC did at the time -- although now it does.

What was once: std::allocator<T>::rebind<U>::type::rebind<V>::typeIs now: typename std::allocator<T>::template rebind<U>::type::template rebind<V>::type


Isn't C++ fun?

This topic is closed to new replies.

Advertisement