Tuesday, March 6, 2018

compiler errors - How to pass normal param as well as template param in a template function in C++?

I have a template function (as follows) in a namespace called myNamespace:


template 
void getRandomItems(NaturalNumber size, setX &random, setX &items)
{
assert(size <= items.size());
//set of randomly selected indices for items
set 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);
}
}

However whenever I call this function I get these errors:



generic.h: In function ‘void myNamespace::getRandomItems(NaturalNumber, setX&, setX&) [with setX = std::set >, NaturalNumber = long unsigned int]’:
synthetic-graph.C:87:55: instantiated from here
generic.h:74:32: error: no match for ‘operator=’ in ‘it = index.std::set::begin [with _Key = long unsigned int, _Compare = std::less, _Alloc = std::allocator, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator]()’
/usr/include/c++/4.5/bits/stl_tree.h:224:5: note: candidate is: std::_Rb_tree_const_iterator >& std::_Rb_tree_const_iterator >::operator=(const std::_Rb_tree_const_iterator >&)
synthetic-graph.C:87:55: instantiated from here
generic.h:74:32: error: no match for ‘operator!=’ in ‘it != index.std::set<_Key, _Compare, _Alloc>::end [with _Key = long unsigned int, _Compare = std::less, _Alloc = std::allocator, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator]()’
/usr/include/c++/4.5/bits/stl_tree.h:291:7: note: candidate is: bool std::_Rb_tree_const_iterator<_Tp>::operator!=(const std::_Rb_tree_const_iterator<_Tp>::_Self&) const [with _Tp = std::basic_string, std::_Rb_tree_const_iterator<_Tp>::_Self = std::_Rb_tree_const_iterator >]
generic.h:77:4: error: cannot convert ‘const std::basic_string’ to ‘NaturalNumber’ in assignment

I have tried all combinations but no luck, please help me!!!

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...