Iterator accessing problem in vector

Hi everyone,

While using iterator in vector template, I am facing compiler error

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
class CTest
{
public:
	void Insert(T&	value);
	void Display();
	void Erase(T& value);

private:
	std::vector<T>			myVecContainer;
 	myVecContainer::iterator	myVecItertor;   // Error.
};


error C2653: 'myVecContainer' : is not a class or namespace name
see reference to class template instantiation 'CTest<T>' being compiled.

Yes, the template type for the vector is not initialized. Please let me know, how can we use iterator for vector using templates.
myVecContainer is a variable. You can't use :: with that.

typename std::vector<T>::iterator myVecIterator is the right way.
Last edited on
Thanks hamsterman; yes, it is variable & i shouldn't use it. But still my problem persist. Can you please point it? I am a beginner & learning templates.

1
2
3
4
5
typedef std::vector<T> 	     myVecContainer;
typedef myVecContainer::iterator myVecIterator;   // Error

myVecContainer m_VecContainer;
myVecIterator  m_VecIterator;  // Error  
typedef typename myVecContainer::iterator myVecIterator;
Thanks Peter87 & it compiles successfully. Please explain why we need to include "typename" in the code?
The compiler doesn't know if std::vector<T>::iterator is a type or not. Someone could make a template specialization for std:vector<int> and make std:vector<int>::iterator a function, variable or something else, so what it is depends on the type T. typename is needed here to tell the compiler that it is a type, otherwise it will assume it's not a type.
Thank you so much Peter87.
Topic archived. No new replies allowed.