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

,當我使用錯誤:「從屬名稱不是類型」。當類作爲返回值類型定義使用類型,與模板

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

VC++報錯爲 Source.cpp(207):警告C4346: '袋:: SIZE_TYPE':依賴名稱不是一種類型

有人可以告訴我爲什麼嗎?謝謝!

+1

的[地點和原因可能重複做我必須把 「模板」 和 「類型名」 關鍵詞?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – juanchopanza 2013-04-21 13:58:43

回答

27

應該

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

很好。有用。謝謝! – 2013-04-21 14:04:54

25

您需要bag<Item>::size_type之前預先考慮typename,因爲它是一個依賴型

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

按照C++的11個標準:

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 .

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

+0

謝謝!我遵循你的建議並解決了我的問題。 – 2013-04-21 14:06:03