我创建了一个函数find_all,它返回STL容器xr
中元素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);
^
我想简单地纠正它。