Jump to content
  • Advertisement
Sign in to follow this  
comedy_17

Xcode + template pointer vector iterator = error?

This topic is 3539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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>::type
Is now: typename std::allocator<T>::template rebind<U>::type::template rebind<V>::type


Isn't C++ fun?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!