Hello everyone,

I'm having problems with this code:

#include <vector>
using namespace std;

template <class Type>
class Some
{
	private:
		vector<Type> elements;
		vector<Type>::size_type index; //THIS IS WRONG
	public:
		Some() { }
};


int main()
{
}

It doesn't compile, giving me

p.cpp:9: error: type ‘std::vector<Type, std::allocator<_Tp1> >’ is not derived from type ‘Some<Type>’

while this code (after changing Type to int) compiles

#include <vector>
using namespace std;

template <class Type>
class Some
{
	private:
		vector<Type> elements;
		vector<int>::size_type index; //Type changed to int
	public:
		Some() { }
};


int main()
{
}

What's wrong?

Thanks in advance.

This is what is called a dependent type because the type of the vector size_type is dependent on is also dependent on a template argument. It is fixed with the typename keyword.

template <class Type>
class Some
{
	private:
		vector<Type> elements;
		typename vector<Type>::size_type index;
	public:
		Some() { }
};

When Type is replaced with int, size_type is no longer a dependent type because there is no dependency on a template argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.