0

好的,所以我们知道 STL 中的函数,例如

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

我正在使用一个也适用于容器的方法来开发一个类,并且我意识到我也可以像上面的示例中那样对其进行模板化。非模板化版本是这样的:

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

我的目标是将其更改为:

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

但是我在制定实施细节时遇到了麻烦:

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;
}

所以问题是如何根据方法的模板化参数正确实例化模板化迭代器?请注意,我只需要一个输入迭代器。

4

1 回答 1

5

你的测试用例不完整,所以我得请教一下我的水晶球。

您已将模板定义放在源代码文件中,而它本应放在头文件中。

参见:为什么模板只能在头文件中实现?

于 2012-05-08T17:38:38.100 回答