Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

conversion from std::vector<char>::iterator to char *

0 views
Skip to first unread message

Edward Diener

unread,
Feb 16, 2004, 9:32:25 PM2/16/04
to
I have a std::vector<char> variable and a std::vector<char>::iterator
variable set to somewhere in the former variable. I would like to convert
the latter to a char * for a function which expects it as its parameter. How
do I do it ? My attempts using static_cast and reinterpret_cast fail.

#include <vector>

std::vector<char> x;
/* x is filled with values */
void someFunction(char *);
std::vector<char>::iterator xit(x.begin());

someFunction(static_cast<char *>(xit));

MyFile.cpp(7) : error C2440: 'static_cast' : cannot convert from
'std::vector<_Ty>::iterator' to 'const char *'
with
[
_Ty=char
]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called

someFunction(reinterpret_cast<char *>(xit));

MyFile.cpp(7) : error C2440: 'reinterpret_cast' : cannot convert from
'std::vector<_Ty>::iterator' to 'char *'
with
[
_Ty=char
]
Conversion requires a constructor or user-defined-conversion operator, which
can't be used by const_cast or reinterpret_cast


Pete Becker

unread,
Feb 16, 2004, 9:38:25 PM2/16/04
to
Edward Diener wrote:
>
> std::vector<char> x;
> /* x is filled with values */
> void someFunction(char *);
> std::vector<char>::iterator xit(x.begin());
>
> someFunction(static_cast<char *>(xit));
>

someFunction(&*xit);

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Edward Diener

unread,
Feb 16, 2004, 9:40:10 PM2/16/04
to
Never mind, I woke up:

someFunction(&(*xit));

TT (Tom Tempelaere)

unread,
Feb 17, 2004, 6:30:05 AM2/17/04
to
"Edward Diener" <eddi...@tropicsoft.com> wrote in message
news:uqWGG5P9...@TK2MSFTNGP10.phx.gbl...

> I have a std::vector<char> variable and a std::vector<char>::iterator
> variable set to somewhere in the former variable. I would like to convert
> the latter to a char * for a function which expects it as its parameter.
How
> do I do it ? My attempts using static_cast and reinterpret_cast fail.

To get a pointer to the object an iterator is pointing at, use the
following:

&(*iter)

> #include <vector>
>
> std::vector<char> x;
> /* x is filled with values */
> void someFunction(char *);
> std::vector<char>::iterator xit(x.begin());
>
> someFunction(static_cast<char *>(xit));
>
> MyFile.cpp(7) : error C2440: 'static_cast' : cannot convert from
> 'std::vector<_Ty>::iterator' to 'const char *'
> with
> [
> _Ty=char
> ]
> No user-defined-conversion operator available that can perform this
> conversion, or the operator cannot be called
>
> someFunction(reinterpret_cast<char *>(xit));
>
> MyFile.cpp(7) : error C2440: 'reinterpret_cast' : cannot convert from
> 'std::vector<_Ty>::iterator' to 'char *'
> with
> [
> _Ty=char
> ]
> Conversion requires a constructor or user-defined-conversion operator,
which
> can't be used by const_cast or reinterpret_cast

Why didn't you use std::string?

---
Cheers,
Tom Tempelaere


Darkay Li

unread,
Mar 11, 2004, 3:03:01 AM3/11/04
to
coz string dosn't assure continuous memory allocation.
"TT (Tom Tempelaere)" <_N_OSPAM...@hotmail.comMAPSO_N_> 写入邮件
news:1XmYb.4448$Js7.2...@phobos.telenet-ops.be...

Darkay Li

unread,
Mar 11, 2004, 3:04:57 AM3/11/04
to
I like to use like follow :
if(!x.empty())
someFunction(&x[0]);
else
// x is empty

"Edward Diener" <eddi...@tropicsoft.com> 写入邮件
news:uqWGG5P9...@TK2MSFTNGP10.phx.gbl...

0 new messages