Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » cannot convert from 'std::string' to 'void *'

This thread is locked; no one can reply to it. rss feed Print
cannot convert from 'std::string' to 'void *'
alex Ioan
Member #12,015
June 2010

error C2440: '=' : cannot convert from 'std::string' to 'void *'

code:

string name;
peer -> data = name;

So... how to make this work ?
I basically have to convert string to void and void to string afterwards...
tried "peer -> data = &name;" but that adds letters and numbers before the actual string...

and the definition of peer: (part of it)

typedef struct _ENetPeer
{ ...
...
void * data;
...

and I CANT modify the definition of peer.

Any help is appreciated !

Thomas Fjellstrom
Member #476
June 2000
avatar

You could try looking up the c_str member of std::string.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

J-Gamer
Member #12,491
January 2011
avatar

The compiler throws this error because you are trying to compare an object with a pointer. If you really want to, you can change the string like this:
std::string* name;
But I don't get why you would want to change it to a void pointer.

/edit:
I was using the wrong words(compare instead of assign) and I didn't fully read his post. nvm

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

BAF
Member #2,981
December 2002
avatar

Sorry, but you are totally wrong. He's not comparing anything, and changing name to be a string pointer won't help much.

The reason for a void pointer is because he's using a library that lets you store your own objects, and the only practical way of allowing this in C/C++ is via void pointers.

ImLeftFooted
Member #3,935
October 2003
avatar

peer->data = malloc(name.size() + 1);
strcpy(peer->data, name.c_str());

...

cout << "Peer: " << (char*)peer->data << endl;

alex Ioan
Member #12,015
June 2010

@ Dustin Dettmer

That does not work...
it gives me :

'strcpy' : cannot convert parameter 1 from 'void *' to 'char *'

Dennis
Member #1,090
July 2003
avatar

alex Ioan
Member #12,015
June 2010

@Denis

Tried static cast but did not work...

Dennis
Member #1,090
July 2003
avatar

It should work: http://en.wikipedia.org/wiki/Type_conversion#Explicit_type_conversion_in_C-like_languages

peer->data = (void*)name.c_str();
cout << "Peer->data: " << (char*)peer->data << endl;

alex Ioan
Member #12,015
June 2010

that worked... thanks !
I was trying
peer->data = reinterpret_cast<void*>(&name);
or
peer->data = reinterpret_cast<void*>(name);
and was not giving me anything

Dennis
Member #1,090
July 2003
avatar

Well that's strange, because that should work just fine as well (first version with the &name).

What do you mean by "was not giving me anything"? If you mean trying to display the data field as a C-string afterwards revealed nothing then I understand.

Consider the following example:

void *data = NULL;
string name("Dennis");
data = reinterpret_cast<void*>(&name);

This compiles and runs just fine. But outputting data as a C string won't work, because all it holds is the address of the name string, which is a C++ datatype and not just a pointer to a zero terminated string, so it contains other data before the actual character data. I've checked in my environment during debug and the first memory byte at the address of name was 0 (which in case of interpreting the data at that address as a zero terminated C "string" (not to confuse with the string type from C++) explains why trying to display that reveals nothing).

ImLeftFooted
Member #3,935
October 2003
avatar

peer->data = malloc(name.size() + 1);
strcpy((char*)peer->data, name.c_str());

...

cout << "Peer: " << (char*)peer->data << endl;

Go to: