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

und wenn ichFehler: "abhängiger Name ist kein Typ". Bei Verwendung typedef Typ in der Klasse als Rückgabewert, mit Vorlage

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

VC++ Bericht Fehler als Source.cpp (207): Warnung C4346: 'bag :: size_type': abhängiger Name ist kein Typ

Kann mir jemand zeigen, warum? Vielen Dank!

+1

möglich Duplikat [Wo und warum muss ich die "Vorlage" setzen und "Typname" keywords ?] (http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – juanchopanza

Antwort

27

Es sollte

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

groß. Es klappt. Vielen Dank! –

25

Sie müssen typename vor bag<Item>::size_type vorangestellt wird, da es ein abhängiger Typ ist.

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

Gemäß der 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 .

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

+0

Danke! Ich folgte deinem Vorschlag und habe mein Problem behoben. –

Verwandte Themen