我在名为 myNamespace 的命名空间中有一个模板函数(如下):
template <typename setX>
void getRandomItems(NaturalNumber size, setX &random, setX &items)
{
assert(size <= items.size());
//set of randomly selected indices for items
set<NaturalNumber> index;
NaturalNumber r, i;
while(index.size() < size)
{
r = unifRand(0,items.size()-1);
index.insert(r);
}
typename setX::iterator it, sit = items.begin();
for(i = 0, it = index.begin(); it != index.end(); it ++)
{
//find the r-th elt in index
r = *it;
for(; i < r; i ++)
sit++;
random.insert(*sit);
}
}
但是,每当我调用此函数时,我都会收到以下错误:
generic.h:在函数 'void myNamespace::getRandomItems(NaturalNumber, setX&, setX&) [with setX = std::set<std::basic_string<char>>, NaturalNumber = long unsigned int]'中: 合成图.C:87:55: 从这里实例化 generic.h:74:32: 错误: 'it = index.std::set::begin [with _Key = long unsigned int, _Compare = std::less<long unsigned int> 中的 'operator=' 不匹配, _Alloc = std::allocator<long unsigned int>, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<long unsigned int>]()' /usr/include/c++/4.5/bits/stl_tree.h:224:5:注意:候选是:std::_Rb_tree_const_iterator<std::basic_string<char> >& std::_Rb_tree_const_iterator<std::basic_string<char> >::operator=(const std::_Rb_tree_const_iterator<std::basic_string<char> >&) 合成图.C:87:55: 从这里实例化 generic.h:74:32: 错误:'it != index.std::set<_Key, _Compare, _Alloc>::end [with _Key = long unsigned int, _Compare = std ::less<long unsigned int>, _Alloc = std::allocator<long unsigned int>, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<long unsigned int>]()' /usr/include/c++/4.5/bits/stl_tree.h:291:7:注意:候选是:bool std::_Rb_tree_const_iterator<_Tp>::operator!=(const std::_Rb_tree_const_iterator<_Tp>::_Self&) const [with _Tp = std::basic_string<char>, std::_Rb_tree_const_iterator<_Tp>::_Self = std::_Rb_tree_const_iterator<std::basic_string<char> >] generic.h:77:4:错误:无法在赋值中将 'const std::basic_string<char>' 转换为 'NaturalNumber'
我已经尝试了所有组合,但没有运气,请帮助我!!!