Home > Archive > VC STL > March 2006 > vector.insert compile failure in VS2005
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
vector.insert compile failure in VS2005
|
|
| Vincent Finn 2006-03-08, 7:58 am |
| Hi,
I am trying to get my code building on VS2005 and have hit a problem.
The following code doesn't compile
// begin code
std::vector<std::string> asWords;
asWords.push_back("aaaaaaaa");
asWords.push_back("bbbbbbbb");
unsigned int iIndex = 0;
asWords.insert(&asWords[iIndex + 1], asWords[iIndex]);
// end code
The error suggests that it can't find a constructor to take an item
from the vector and put it back in again?
// begin error
d:\temp\test2005\test2005\test2005.cpp(14) : error C2664:
'std::_Vector_iterator<_Ty,_Alloc>
std::vector<_Ty>::insert(std::_Vector_iterator<_Ty,_Alloc>,const _Ty
&)' : cannot convert parameter 1 from
'std::basic_string<_Elem,_Traits,_Ax> *__w64 ' to
'std::_Vector_iterator<_Ty,_Alloc>'
with
[
_Ty=std::string,
_Alloc=std::allocator<std::string>
]
and
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
and
[
_Ty=std::string,
_Alloc=std::allocator<std::string>
]
No constructor could take the source type, or constructor overload
resolution was ambiguous
// end error
Any ideas what might be wrong?
Vin
| |
| Pete Becker 2006-03-08, 7:58 am |
| Vincent Finn wrote:
>
> The error suggests that it can't find a constructor to take an item
> from the vector and put it back in again?
>
The error says that it can't convert a pointer into an iterator:
> cannot convert parameter 1 from
> 'std::basic_string<_Elem,_Traits,_Ax> *__w64 ' to
> 'std::_Vector_iterator<_Ty,_Alloc>'
The error message is correct. You need an iterator pointing at the
position for the insert. Change &asWords[Index + 1] (the pointer) to
asWords.begin() + Index + 1 (an iterator).
--
Pete Becker
Roundhouse Consulting, Ltd.
| |
| Vincent Finn 2006-03-08, 7:03 pm |
| On Wed, 08 Mar 2006 08:08:13 -0500, Pete Becker <petebecker@acm.org>
wrote:
>Vincent Finn wrote:
>
>
>The error says that it can't convert a pointer into an iterator:
>
>
>The error message is correct. You need an iterator pointing at the
>position for the insert. Change &asWords[Index + 1] (the pointer) to
>asWords.begin() + Index + 1 (an iterator).
Thanks, the line was so long I missed that (only just noticed the new
word-wrap).
but vector<std::string>::iterator has a constructor
_Vector_iterator(pointer _Ptr).
pointer in this case is std::allocator<std::string>::pointer
which is std::string*
But this constructor is disabled by some new defines
so if I add
#define _SECURE_SCL 0
#define _HAS_ITERATOR_DEBUGGING 0
it'll work.
I'm going to go with that for now. There are too many SCL changes to
do at the moment.
Vin
| |
| Pete Becker 2006-03-08, 7:03 pm |
| Vincent Finn wrote:
>
> But this constructor is disabled by some new defines
> so if I add
> #define _SECURE_SCL 0
> #define _HAS_ITERATOR_DEBUGGING 0
>
> it'll work.
>
Don't fight the system. The versio of insert that you're using takes an
iterator. Give it one.
--
Pete Becker
Roundhouse Consulting, Ltd.
| |
| Jeff F 2006-03-08, 7:03 pm |
| Pete Becker wrote:
> Vincent Finn wrote:
>
>
> Don't fight the system. The versio of insert that you're using takes
> an iterator. Give it one.
I was under the impression that pointers are valid random access iterators,
and that the following code is valid.
int vals[4] = { 1, 2, 3, 4 };
std::vector<int> lInts( &vals[0], &vals[4] );
Jeff Flinn
| |
| Jeff F 2006-03-08, 7:03 pm |
| Pete Becker wrote:
> Vincent Finn wrote:
>
>
> Don't fight the system. The versio of insert that you're using takes
> an iterator. Give it one.
Sorry, Pete I missed your mention of 'insert' above, you are of course,
correct.
Thanks, Jeff
| |
| Igor Tandetnik 2006-03-08, 7:03 pm |
| Jeff F <not@anywhere.com> wrote:
> I was under the impression that pointers are valid random access
> iterators, and that the following code is valid.
>
> int vals[4] = { 1, 2, 3, 4 };
>
> std::vector<int> lInts( &vals[0], &vals[4] );
Pointers are random access iterators _into an array_. Just because X is
a random access iterator into some container does not mean it can be
used to point into any container. For example, you probably wouldn't
expect this to work:
vector<int> v;
deque<int> d;
v.insert(d.begin(), 1);
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Pete Becker 2006-03-08, 7:03 pm |
| Jeff F wrote:
>
> I was under the impression that pointers are valid random access iterators,
> and that the following code is valid.
>
Pointers satisfy the requirements for random access iterators. However,
to access the contents of a container you have to use the container's
iterator type. Some implementations of vector use a pointer and some
don't. The actual type of the iterator is vector<whatever>::iterator. In
your code it's not necessary to use that typedef, though.
vector<int>::begin() returns an iterator of the appropriate type, and
you can add the appropriate offset to it.
--
Pete Becker
Roundhouse Consulting, Ltd.
| |
| Pete Becker 2006-03-08, 7:03 pm |
| Jeff F wrote:
>
> Sorry, Pete I missed your mention of 'insert' above, you are of course,
> correct.
>
That's okay, I didn't read your example before posting my latest
response. There are a couple of member functions that are templates, so
they can take arbitrary iterator types; but whenever you need to refer
to a position in the container you need an iterator of the container's
iterator type.
--
Pete Becker
Roundhouse Consulting, Ltd.
| |
| Vincent Finn 2006-03-09, 7:58 am |
| On Wed, 08 Mar 2006 11:11:42 -0500, Pete Becker <petebecker@acm.org>
wrote:
>Don't fight the system. The versio of insert that you're using takes an
>iterator. Give it one.
I know, resistence is fultile!
But I'll hold out for a few w s until the more important changes are
dealt with, and I have a compile of the full code base, there are lots
of things need changing.
Thanks for the help, Vin
|
|
|
|
|