带迭代器的模板函数

ade*_*rtc 2 c++ templates iterator compiler-errors

我一直在努力做Ex.10-02在Accelerated C++中,它给了我错误,我最终保持"简化"我的程序,直到我达到这一点,甚至它仍然不会编译(通过g ++)给我错误:

test.cpp: In function ‘int main()’:
test.cpp:22: error: no matching function for call to ‘dumb(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >)’
Run Code Online (Sandbox Code Playgroud)

这是我的计划:

#include <algorithm>
#include <iostream>
#include <vector>

using std::cout;    using std::endl;
using std::vector;

template <class Ran, class T> T dumb(Ran begin, Ran end)
{
    return *begin;
}

int main()
{
    vector<int> myVector;
    for (int i = 1; i <= 9; ++i)
        myVector.push_back(i);

    int d = dumb(myVector.begin(), myVector.end());
    cout << "Value = " << d << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

是什么导致了这个错误?

Bjö*_*lex 5

编译器无法在此处推断返回类型.实际上这里不需要将return-type设为template-parameter:

template <class Ran> typename Ran::value_type dumb(Ran begin, Ran end)
{
    return *begin;
}
Run Code Online (Sandbox Code Playgroud)