2

I wrote a function template f which takes a vector iterator as a parameter:

#include<vector>
using namespace std;

template <typename T>
void f(vector<T>::iterator it)
{

}

int main()
{
    vector<int> v;
    f(v.begin());
}

Compiling it with gcc failed, the error message is very hard to understand:

enter image description here

What's the cause of the compilation error and how can I fix it?

abigaile
  • 181
  • 5
  • 1
    You haven't given the parameter passed to f a name. – SPlatten Jan 28 '20 at 08:10
  • 3
    Please don't post pictures of error messages — they're hard to read, let alone understand, especially on cell phones. – Jonathan Leffler Jan 28 '20 at 08:11
  • 1
    The error message you show doesn't match the error output you show. Please create a proper [mcve], and copy-paste (as text!) the full and complete error output of that. – Some programmer dude Jan 28 '20 at 08:12
  • 2
    @Someprogrammerdude [But it does](https://coliru.stacked-crooked.com/a/b4469323f8bd5cbe). – jrok Jan 28 '20 at 08:14
  • @jrok In a way yes, but the shown code doesn't have a call `f(it)` on line 17, and `f` is not on line 6. – Some programmer dude Jan 28 '20 at 08:16
  • @abigaile: https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords also applies. – jrok Jan 28 '20 at 08:16
  • @Someprogrammerdude Good eye, mate ;) – jrok Jan 28 '20 at 08:18
  • You need to declare `template void f(typename vector::iterator)` and call it via `f(v.begin())`, since the compiler can't deduce the template argument. See the [Live example](https://godbolt.org/z/xrsZn2). – StefanKssmr Jan 28 '20 at 09:09

0 Answers0