gpt4 book ai didi

c++ - 类中的typedef

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:31:38 24 4
gpt4 key购买 nike

有这样的代码:

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

请关闭它。

最佳答案

问题是 c::iterator 是一个 qualified-idc 的类型取决于模板参数。根据 §14.6/3:

When a qualified-id is intended to refer to a type that is not a member of the current instantiation and its nested-name-specifier refers to a dependent type, it shall be prefixed by the keyword typename ...

关于c++ - 类中的typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7722276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com