C ++ - 迭代器向量的迭代器,不进行编译

时间:2014-04-07 17:01:48

标签: c++ templates iterator stdvector

我有一个带有const_iterator的模板化类P,我试图创建所述迭代器的向量并迭代该向量:

std::vector<typename P<A, B>::const_iterator>::const_iterator it;

问题是当我尝试编译时我得到

error: expected ‘;’ before ‘it’

关于为什么会发生这种情况的任何想法?

2 个答案:

答案 0 :(得分:2)

typename之前需要std::vector<>,因为P<A, B>中至少有一个模板参数是从属类型:

typename std::vector<typename P<A, B>::const_iterator>::const_iterator it;

答案 1 :(得分:1)

const_iterator的两种用法都取决于模板参数;所以两者都需要typename

typename std::vector<typename P<A, B>::const_iterator>::const_iterator it;
^^^^^^^^             ^^^^^^^^