我正在转换两个现有函数,这两个函数检查迭代器是std::map
中模板的最后一项和倒数第二项。 isLastItem
模板已编译,isSecondLastItem
失败并显示错误消息,我不明白。
导致错误的一行是typename T::reverse_iterator secondLastIt = collection.rbegin();
但模板和非模板版本看起来完全相同。
typedef std::map<time_t, std::string> TInfoMap;
class MainClass
{
public:
MainClass() {};
void test();
private:
/* Doesn't work */
template <typename T>
bool
isSecondLastTemplate(const T &collection, const typename T::const_iterator &searchItem)
{
typename T::reverse_iterator secondLastIt = collection.rbegin();
return true;
}
/* this works */
bool
isSecondLastNoTemplate(const TInfoMap &infoMap, const TInfoMap::const_iterator &searchItem)
{
TInfoMap::reverse_iterator secondLastIt = infoMap.rbegin();
return true;
}
/* This works */
template <typename T>
bool
isLastItem(const T &collection, const typename T::const_iterator &searchItem)
{
if (collection.size() == 1)
return true;
// point to last item
typename T::const_iterator lastItem = collection.end();
--lastItem;
return searchItem == lastItem;
}
private:
TInfoMap mInfoMap;
};
void
MainClass::test()
{
mInfoMap[1] = "One";
mInfoMap[2] = "Two";
mInfoMap[3] = "Three";
TInfoMap::iterator it = mInfoMap.begin();
if (isLastItem(mInfoMap, it))
printf("Last Item!\n");
if (isSecondLastTemplate(mInfoMap, it))
printf("Second Last!\n");
if (isSecondLastNoTemplate(mInfoMap, it))
printf("Second Last!\n");
}
int
main(int argc, char **argv)
{
MainClass theStuff;
theStuff.test();
}
编译错误:
In constructor ‘std::reverse_iterator<_Iterator>::reverse_iterator(const std::reverse_iterator<_Iter>&)
[with _Iter = std::_Rb_tree_const_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >,
_Iterator = std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’:
junk3.cpp:19: instantiated from here
error: no matching function for call to ‘std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::_Rb_tree_iterator(std::_Rb_tree_const_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)’
candidates are:
std::_Rb_tree_iterator<_Tp>::_Rb_tree_iterator(std::_Rb_tree_node<_Tp>*) [with _Tp = std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
std::_Rb_tree_iterator<_Tp>::_Rb_tree_iterator() [with _Tp = std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >::_Rb_tree_iterator(const std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)
答案 0 :(得分:0)
自const_reverse_iterator
collection
const
template <typename T>
bool
isSecondLastTemplate(const T &collection, const typename T::const_iterator &searchItem)
{
typename T::const_reverse_iterator secondLastIt = collection.rbegin();
^^^^^^^^^^^^^^^^^^^^^^
return true;
}