2013-04-21 10 views
17
template <class Item> 
class bag 
{ 
public: 
    //TYPEDEF 
    typedef size_t size_type; 
    typedef Item value_type; 
... 
} 

ve ben Source.cpp olarakHATA: "bağımlı ad bir tür değil". Ne zaman şablonla dönüş değeri olarak sınıfta kullanılması typedef tipi,

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

VC++ rapor hatası kullandığınızda (207): C4346 Uyarı: 'torba :: size_type': bağımlı ad, bir tür

türünde biri Neden biri bana gösterebilir? Teşekkürler!

+1

olası yinelenen ben "şablon" ve "typename" anahtar koymak gerekiyor ?] (http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – juanchopanza

cevap

27

Bu bir bağımlı tip olduğu gibi bag<Item>::size_type önce typename prepend gerekir

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

harika. İşe yarıyor. Teşekkürler! –

25

olmalıdır.

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

C++ 11 Standardı gereğince:

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?

[Nerede ve niçin
+0

Teşekkür ederiz! Önerinizi takip ettim ve sorunumu düzelttim. –

İlgili konular