【问题标题】:ERROR: "dependent name is not a type". When use typedef type in class as return value, with template错误:“从属名称不是类型”。当在类中使用 typedef 类型作为返回值时,带有模板
【发布时间】:2013-04-21 13:50:47
【问题描述】:
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: 'bag::size_type' : 从属名称不是类型

谁能告诉我为什么?谢谢!

【问题讨论】:

标签: c++


【解决方案1】:

应该是

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

【讨论】:

    【解决方案2】:

    您需要在bag&lt;Item&gt;::size_type 之前添加typename,因为它是一个依赖类型。

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

    按照 C++11 标准:

    14.6 名称解析

    在模板声明或定义中使用并且依赖于模板参数的名称被假定为不命名类型,除非适用的名称查找找到类型名称或该名称由关键字 typename 限定。

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

    【讨论】:

    • 谢谢!我按照您的建议解决了我的问题。
    猜你喜欢
    • 2018-06-28
    • 2012-08-11
    • 2017-04-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多