Question

I have the following template function:

template<class In, class Out>
Out copy(In begin, In end, Out dest)
{
    while(begin != end)
        *dest++ = *begin++;

    return dest;
}

and when I call the following:

static const double arr[] = {50,23, 56,1,78,23,25,26,143,120};
vector<double> values(arr, arr + sizeof(arr) / sizeof(arr[0]) );


// Using copy
// ----------
vector<double> values_copy;
copy(values.begin(), values.end(), back_inserter(values_copy));

for(int i=0 ; i < values_copy.size(); i++)
    cout << values_copy[i] << endl;

I get the following compile error (btw to the g++ developers, thank you):

2-iterator.cpp: In function ‘int main()’:
2-iterator.cpp:77:63: error: call of overloaded ‘copy(std::vector<double>::iterator, std::vector<double>::iterator, std::back_insert_iterator<std::vector<double> >)’ is ambiguous
2-iterator.cpp:77:63: note: candidates are:
2-iterator.cpp:41:5: note: Out copy(In, In, Out) [with In = __gnu_cxx::__normal_iterator<double*, std::vector<double> >, Out = std::back_insert_iterator<std::vector<double> >]
/usr/include/c++/4.6/bits/stl_algobase.h:444:5: note: _OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<double*, std::vector<double> >, _OI = std::back_insert_iterator<std::vector<double> >]
[Finished in 0.7s with exit code 1]
Was it helpful?

Solution

Remove:

using namespace std;

and use std::cout, std::endl, etc. There is already std::copy function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top