0

I have a template function (as follows) in a namespace called 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);
    }
}

However whenever I call this function I get these errors:

generic.h: In function ‘void myNamespace::getRandomItems(NaturalNumber, setX&, setX&) [with setX = std::set<std::basic_string<char> >, 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<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:224:5: note: candidate is: 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> >&)
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<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: 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<char>, std::_Rb_tree_const_iterator<_Tp>::_Self = std::_Rb_tree_const_iterator<std::basic_string<char> >]
generic.h:77:4: error: cannot convert ‘const std::basic_string<char>’ to ‘NaturalNumber’ in assignment

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

1
  • 1
    I have tried all combinations: show us what combinations you tried Commented Jul 11, 2011 at 21:53

2 Answers 2

1

setX is not a set of NaturalNumbers so the iterators aren't compatible when you say it = index.begin(). You could possibly make it an iterator of set<NaturalNumber> instead, I can't quite make out what you really want to do here.

Also I noticed that in your inner loop you don't do any checks to make sure sit doesn't run off the end of its set.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @mark-b and @sehe. Another question relevant to this is: how can I pass set of a template data type as an argument of a function like this: template <class X> void func(set<X> myset); But this shows errors. How can I do that?
1

You're trying to assign incompatible iterators.

Perhaps you meant

set<NaturalNumber>::iterator it;
typename setX::iterator sit = items.begin();

instead of

typename setX::iterator it, sit = items.begin();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.