0

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Luther von Wulf
1

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.

Edited by Luther von Wulf: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.