Click to See Complete Forum and Search --> : Question about front_inserter of STL.


George2
February 12th, 2003, 08:04 AM
Hi, everyone!

Here is a sample of the usage of inserter of STL.
When compiling it with VC6.0, some error occur.

I have added the source codes and related error
messages below.

Source:

--------
#include <iostream>
#include <vector>
#include <deque>
#include <fstream>
#include <algorithm>

using namespace std;


template <class Container>
back_insert_iterator<Container> back_inserter(Container& x) {
return back_insert_iterator<Container>(x);
}

template <class Container>
front_insert_iterator<Container> front_inserter(Container& x) {
return front_insert_iterator<Container>(x);
}

template <class Container, class Iterator>
insert_iterator<Container> inserter(Container& x, Iterator i) {
return insert_iterator<Container>(x, Container::iterator(i));
}

int main()
{
ifstream f ("c:\\example"); // file example: 1 3
deque<int> d;
copy (istream_iterator<int>(f),
istream_iterator<int>(),
back_inserter(d) );

vector<int> w (2,7);
copy (w.begin(), w.end(), front_inserter (d) );

insert_iterator<deque<int> > i = inserter (d, ++d.begin() );
*i = 9;

return 1;
}
--------

Error messages:
--------
C:\Program Files\Microsoft Visual Studio\MyProjects\testI\testI.cpp(31) : error C2667: 'back_inserter' : none of 2 overload

have a best conversion
C:\Program Files\Microsoft Visual Studio\MyProjects\testI\testI.cpp(31) : error C2668: 'back_inserter' : ambiguous call to

overloaded function
C:\Program Files\Microsoft Visual Studio\MyProjects\testI\testI.cpp(34) : error C2667: 'front_inserter' : none of 2 overload

have a best conversion
C:\Program Files\Microsoft Visual Studio\MyProjects\testI\testI.cpp(34) : error C2668: 'front_inserter' : ambiguous call to

overloaded function
C:\Program Files\Microsoft Visual Studio\MyProjects\testI\testI.cpp(36) : error C2667: 'inserter' : none of 2 overload have a

best conversion
C:\Program Files\Microsoft Visual Studio\MyProjects\testI\testI.cpp(36) : error C2668: 'inserter' : ambiguous call to

overloaded function
C:\Program Files\Microsoft Visual Studio\MyProjects\testI\testI.cpp(36) : error C2512: 'insert_iterator<class

std::deque<int,class std::allocator<int> > >' : no appropriate default constructor available
Error executing cl.exe.
--------


Thanks in advance,
George

Philip Nicoletti
February 12th, 2003, 11:29 AM
back_inserter() etc. exist in namespace std. You
are re-defining them in global namespace. The
compiler is not able to determine which it
should use (yours or the ones in std).

Here are a few of the ways to fix this:

1) just get rid of all your functions

2) change the name of your functions (i.e. my_back_inserter)

3) explicitly tell the compiler to use yours :


copy (istream_iterator<int>(f), istream_iterator<int>(), ::back_inserter(d) );


4) put your functions in your own namespace

George2
February 12th, 2003, 08:59 PM
Thanks, Philip buddie!

George

Originally posted by Philip Nicoletti
back_inserter() etc. exist in namespace std. You
are re-defining them in global namespace. The
compiler is not able to determine which it
should use (yours or the ones in std).

Here are a few of the ways to fix this:

1) just get rid of all your functions

2) change the name of your functions (i.e. my_back_inserter)

3) explicitly tell the compiler to use yours :


copy (istream_iterator<int>(f), istream_iterator<int>(), ::back_inserter(d) );


4) put your functions in your own namespace