-
March 21st, 2009, 01:24 PM
#1
[RESOLVED] Template Horrors
It seems all my problems involve templates. Anyways, my problem is whenever and wherever I put:
Code:
std::vector<T>::iterator iter;
I get the compiler error:
error: expected ';' before 'iter'
I've tried putting the above code directly in a template method of a template class and directly as a member of the same class in it's definition. Each time I get the same error where it occures. I feel stupid because I think I'm missing something very fundamental here. Can anyone help? Thanks.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 21st, 2009, 01:39 PM
#2
Re: Template Horrors
My guess is that you need to disambiguate with typename since the nested name iterator depends on T, which is presumably the template parameter. For example:
Code:
typename std::vector<T>::iterator iter;
-
March 21st, 2009, 01:43 PM
#3
Re: Template Horrors
Would you mind elaborating a bit on this? I'm not sure what this will do.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 21st, 2009, 01:43 PM
#4
Re: Template Horrors
We need to see the bigger context in which that's being used. Is this inside another template class? etc... Post all relevant code.
gg
-
March 21st, 2009, 01:49 PM
#5
Re: Template Horrors
Well, I don't think much of it is very relevant, but here you go:
Code:
template<class T> class SomeClass
{
public:
void someMethod (void)
{
...
std::vector<T>::iterator iter; // I get the error everywhere. Whether I put it here...
...
}
protected:
std::vector<T>::iterator iter; // .. Or here, it doesn't matter
};
Edit:
It looks like laserlight was right, because it will work now. It's just that I don't understand why I needed to do that.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 21st, 2009, 01:58 PM
#6
Re: Template Horrors
Originally Posted by Etherous
It's just that I don't understand why I needed to do that.
Because std::vector<T>::iterator can be interpreted as a non-type name (e.g., a variable name) or as a type name, and the rules say that it should be interpreted as a non-type name by default.
-
March 21st, 2009, 02:05 PM
#7
Re: Template Horrors
Originally Posted by laserlight
Because std::vector<T>::iterator can be interpreted as a non-type name (e.g., a variable name) or as a type name, and the rules say that it should be interpreted as a non-type name by default.
Okay, this is making more sense. For anyone else with this problem, I just stumbled on a good bit of reading that explains this very well:
http://pages.cs.wisc.edu/~driscoll/typename.html
Last edited by Etherous; March 21st, 2009 at 03:23 PM.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|