I'm reading the book Essential C++ and trying out some of the examples in it. The following code from Chapter 3 was supposed to work.
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
using namespace std;
template <typename IteratorType, typename elemType>
IteratorType find(IteratorType first, IteratorType last, const elemType &value)
{
for (; first != last; ++first)
if (value == *first)
return first;
return last;
};
int main()
{
int asize = 8;
int ia[asize] = {1, 1, 2, 3, 5, 8, 13, 21};
int *pia = find(ia, ia + asize, 1024);
if (pia != ia + asize)
cout<<"found in array"<<endl;
else
{
cout<<"not found in array"<<endl;
}
return 0;
}
However, it's showing the error
call of overloaded ‘find(int [asize], int*, int)’ is ambiguous
Then I replaced
int *pia = find(ia, ia + asize, 1024);
with
int *pia = find(&ia[0], ia + asize, 1024);
Now it's showing the error
call of overloaded ‘find(int*, int*, int)’ is ambiguous
I'm working with Ubuntu 18.04 and G++ 7.5.0.
I would appreciate it if anyone can let me know what the correct implementation should be.