0

Alright, so we know functions in the STL like

std::fill(boolContainer.begin(), boolContainer.end(), false);

I'm working on a class with a method which also works on a container, and I've realized that I just as well might template it like in the example above The non-templated version is like this:

class SomeClass {
public:
    // ...
    int containerMethod(std::vector<int> &v);
    // ...  
private:
    // ...
};

And I'm aiming to change it into:

class SomeClass {
public:
    // ...
    template <class InputIterator>
    int containerMethod(const InputIterator &begin, const InputIterator &end);
    // ...  
private:
    // ...
};

However I'm having trouble working out the details for the implementation:

template <class Iter> int SomeClass::containerMethod
(const Iter &begin, const Iter&end) {
    // Here I need to instantiate an iterator for the container.
    Iter iter;
    for (iter = begin; iter != end; ++iter) {
        // This does not seem to work.
    }
    return 0;
}

So the question is how does one correctly instantiate a templated iterator, based on the templated parameters of a method? Note that I only need an input iterator.

rwols
  • 2,968
  • 2
  • 19
  • 26

1 Answers1

5

Your test case is incomplete, so I have to consult my crystal ball.

You have placed your template definition in a source code file, when it should have been in a header file.

See: Why can templates only be implemented in the header file?

Community
  • 1
  • 1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308