![]() |
|
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; So... how to make this work ? and the definition of peer: (part of it) typedef struct _ENetPeer and I CANT modify the definition of peer. Any help is appreciated !
|
Thomas Fjellstrom
Member #476
June 2000
![]() |
You could try looking up the c_str member of std::string. -- |
J-Gamer
Member #12,491
January 2011
![]() |
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: /edit: " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
BAF
Member #2,981
December 2002
![]() |
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
![]() |
|
alex Ioan
Member #12,015
June 2010
|
@ Dustin Dettmer That does not work... 'strcpy' : cannot convert parameter 1 from 'void *' to 'char *'
|
Dennis
Member #1,090
July 2003
![]() |
alex Ioan
Member #12,015
June 2010
|
@Denis Tried static cast but did not work...
|
Dennis
Member #1,090
July 2003
![]() |
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;
--- 0xDB | @dennisbusch_de --- |
alex Ioan
Member #12,015
June 2010
|
that worked... thanks !
|
Dennis
Member #1,090
July 2003
![]() |
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). --- 0xDB | @dennisbusch_de --- |
ImLeftFooted
Member #3,935
October 2003
![]() |
|
|