类中的Typedef

时间:2011-10-11 06:42:05

标签: c++

有这样的代码:

template <class T>
class SomeClass{
    typedef boost::shared_ptr<T> sPtr;
    typedef std::vector<sPtr> c;
    typedef c::iterator cIt;  // here is the problem
};

,错误是:

main.cpp:23: error: type ‘std::vector<boost::shared_ptr<X>, std::allocator<boost::shared_ptr<X> > >’ is not derived from type ‘SomeClass<T>’
main.cpp:23: error: expected ‘;’ before ‘cIt’

如何在类中使用typedef模板参数?

编辑:

我明白了,因为g ++必须是:

typedef typename c::iterator cIt;  // here is the problem

请关闭它。

1 个答案:

答案 0 :(得分:7)

问题是c::iterator qualified-id c的类型取决于模板参数。根据{{​​1}}:

  

qualified-id 旨在引用不是当前实例化成员的类型且其嵌套名称说明符引用依赖类型时,它应该是   前缀为关键字typename ...