Template Class Question

I am making a Template class and want it to have private iterators on it but there's something I'm missing about syntax and would like guidance for doing this right. I cannot show the entire class as it's proprietary but I can show where I am having issues

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T>
class MyClass
{
private:
	typename map<unsigned, T*>::iterator myIterator;

        // I want two instances of the above iterator but it won't let me make them

        myIterator _it1;
        myIterator _it2;

public:
	MyClass();
        // lots of other code I can't show
};


I'm getting a missing type specifier error for the _it1, _it2 variables. In fact when I remove them I can use the myIterator like a private instance variable:

myIterator = someMap.begin()

What is the proper syntax for these type of private instance iterators?
Also if someone could provide a brief explanation as to why the following doesn't work either I'd appreciate it:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class T>
class MyClass
{
private:
	map<unsigned, T*>::iterator _it1;
        map<unsigned, T*>::iterator _it2;

 

public:
	MyClass();
        // lots of other code I can't show
};
In your first post, line #5 should be:

typedef map<unsigned, T*>::iterator myIteratorType;

And lines 9 and 10 should be:

1
2
myIteratorType _it1;
myIteratorType _it2;


That should work OK. As you showed it (post #1), the token 'myIterator' was a private field of type 'map<unsigned, T*>::iterator'.

Post #2 is probably missing 'typename' in lines 5 and 6.
So it should be typedef typename map<unsigned, T*>::iterator myIteratorType;
dependant name http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18
Topic archived. No new replies allowed.