0

我创建了一个函数 find_all,它返回xrSTL 容器中元素的所有位置的位置(迭代器)向量ar。这段代码有什么问题。

template<typename T1, typename T2 = T1::value_type>
auto find_all(const T1& ar, T2 xr)
{
    typedef T1::const_iterator const_iterator;
    vector<const_iterator> it;
    for (auto it2 = ar.cbegin(); it2 != ar.cend(); ++it2)
        if (*it2 == xr)
            it.push_back(it2);
    return it;
}

该代码在 Visual C++ 2015 中编译,但在 gcc 编译器中出现以下错误:错误:

prog.cpp:3:38: error: need 'typename' before 'T1::value_type' because 'T1' is a dependent scope
  template<typename T1, typename T2 = T1::value_type>
                                      ^
prog.cpp: In function 'auto find_all(const T1&, T2)':
prog.cpp:6:11: error: need 'typename' before 'T1::const_iterator' because 'T1' is a dependent scope
   typedef T1::const_iterator const_iterator;
           ^
prog.cpp:7:10: error: 'const_iterator' was not declared in this scope
   vector<const_iterator> it;
          ^
prog.cpp:7:24: error: template argument 1 is invalid
   vector<const_iterator> it;
                        ^
prog.cpp:7:24: error: template argument 2 is invalid
prog.cpp:7:28: error: invalid type in declaration before ';' token
   vector<const_iterator> it;
                            ^
prog.cpp:10:8: error: request for member 'push_back' in 'it', which is of non-class type 'int'
     it.push_back(it2);
        ^

Ideone 链接

我想简单地纠正它。

4

0 回答 0