【问题标题】:Using template value inside nested class在嵌套类中使用模板值
【发布时间】:2020-09-25 14:32:34
【问题描述】:

我有以下类定义:

template <int B>
class SparseBitvector
{
    typedef typename std::vector<std::bitset<B>> BitsetVector;

public:
    class iterator: public std::iterator<...>
    {
        public:
            explicit iterator(..., BitsetVector::iterator* _chunksIt, ...) {...}
    }
}

编译时出现此错误:

/project/powerset/src/powerset/sparse_bitvector.h:96:125: error: 'std::BitsetVector<std::bitset<(long unsigned int)B>, std::allocator<std::bitset<(long unsigned int)B> > >::iterator' is not a type
         explicit iterator(SortedStaticSet<EHRule> *_offsets, std::vector<std::bitset<B>> *_chunks, SetElement* _offssetsIt, BitsetVector::iterator* _chunksIt, int _indInBitset)

是否允许在嵌套类中使用模板值?

如果没有,我是否还有其他错误?

【问题讨论】:

标签: c++ class templates iterator class-template


【解决方案1】:

您需要使用typename keyword for depended types。以这里为例

typename BitsetVector::iterator* _chunksIt
//^^^^^^

其次,模板类型别名不需要typename

typedef std::vector<std::bitset<B>> BitsetVector;
// or better use
// using BitsetVector = std::vector<std::bitset<B>>;

上述的最小示例考虑如下:

#include <vector>
#include <iterator>
#include <bitset>

template <int B>
class SparseBitvector
{
   typedef std::vector<std::bitset<B>> BitsetVector;

public:
   class iterator : public std::iterator<std::input_iterator_tag, BitsetVector>
   {
   public:
      explicit iterator(std::input_iterator_tag, BitsetVector, typename BitsetVector::iterator* _chunksIt)
      {
      }
   };
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多