Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: "dependent name is not a type". When use typedef type in class as return value, with template

Tags:

c++

template <class Item> class bag { public:     //TYPEDEF     typedef size_t size_type;     typedef Item value_type; ... } 

and when I use

template<class Item> bag<Item>::size_type bag<Item>::count(const Item& target) const 

VC++ report error as Source.cpp(207): warning C4346: 'bag::size_type' : dependent name is not a type

Could anybody show me why? Thanks!

like image 523
Yuan Wang Avatar asked Apr 21 '13 13:04

Yuan Wang


2 Answers

It should be

template<class Item> typename bag<Item>::size_type bag<Item>::count(const Item& target) const 
like image 58
Vaughn Cato Avatar answered Sep 23 '22 06:09

Vaughn Cato


You need to prepend typename before bag<Item>::size_type as it is a dependent type.

typename bag<Item>::size_type bag<Item>::count(const Item& target) const 

As per the C++11 Standard:

14.6 Name resolution

A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename.

Related: Where and why do I have to put the "template" and "typename" keywords?

like image 20
David G Avatar answered Sep 19 '22 06:09

David G