0

I'm trying to implement a simple std::find_if() like function to use (I'm not allowed to use it in my homework).

This is my implementation:

template<class Iterator, class Function>
Iterator setFindIf(Iterator first, Iterator end, Function predicate) {
    for (Iterator iterator = first; iterator != end; ++iterator) {
        if (predicate(*iterator)) {
            return iterator;
        }
    }
    return end;
}

This is the line that calls setFindIf():

if (setFindIf(orders.begin(), orders.end(),
            orderCustomerHasOpenOrder(id, ordNum)) != orders.end()) {

And this is the error:

undefined reference to `std::_Rb_tree_const_iterator<Order> setFindIf<std::_Rb_tree_const_iterator<Order>, orderCustomerHasOpenOrder>(std::_Rb_tree_const_iterator<Order>, std::_Rb_tree_const_iterator<Order>, orderCustomerHasOpenOrder)'

Thanks for helpers.

Eitan
  • 47
  • 6
  • 2
    My guess is that you've put the template implementation in a source file, and tried to call it from a different source file. [You can't do that](http://stackoverflow.com/questions/495021); usually, you need to define templates in headers. – Mike Seymour Jun 18 '13 at 13:39

1 Answers1

0

Looks like you declared the template in a header:

template<class Iterator, class Function>
Iterator setFindIf(Iterator first, Iterator end, Function predicate);

And then put the implementation in a .cpp file, and call it from a different .cpp file. It doesn't work this way with templates.

Mike got there first with his answer, but he made it a comment, so I'm posting this anyway.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157