类中的typedef

4

以下是代码示例:

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

请关闭它。

你展示的代码没问题。你应该提供一个能够重现错误的完整示例。 - Björn Pollex
5
如果您自己找到了解决方案,您应该回答自己的问题,然后接受该答案。 - Björn Pollex
1个回答

7
问题在于c::iterator是一个限定符id(qualified-id),而c的类型取决于模板参数。根据§14.6/3的规定:

限定符id意图引用非当前实例化成员的类型,并且其嵌套名称限定符引用一个依赖类型时,它应该以关键字typename为前缀...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接