<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>cannot convert from &#39;std::string&#39; to &#39;void *&#39;</title>
		<link>http://www.allegro.cc/forums/view/606552</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 28 Feb 2011 02:25:46 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>error C2440: &#39;=&#39; : cannot convert from &#39;std::string&#39; to &#39;void *&#39;</p><p>code:</p><p>string name;<br />peer -&gt; data = name;</p><p>So... how to make this work ? <br />I basically have to convert string to void and void to string afterwards...<br />tried &quot;peer -&gt; data = &amp;name;&quot; but that adds letters and numbers before the actual string... </p><p>and the definition of peer: (part of it)</p><p>typedef struct _ENetPeer<br />{ ...<br />  ...<br />   void *        data;   <br />  ...</p><p>and I CANT modify the definition of peer.</p><p>Any help is appreciated !
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (alex Ioan)</author>
		<pubDate>Sun, 27 Feb 2011 23:20:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You could try looking up the <span class="source-code">c_str</span> member of <span class="source-code">std::string</span>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 27 Feb 2011 23:31:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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:<br /><span class="source-code">std::string<span class="k3">*</span> name<span class="k2">;</span></span><br />But I don&#39;t get why you would want to change it to a void pointer.</p><p>/edit:<br />I was using the wrong words(compare instead of assign) and I didn&#39;t fully read his post. nvm
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sun, 27 Feb 2011 23:34:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sorry, but you are totally wrong. He&#39;s not comparing anything, and changing name to be a string pointer won&#39;t help much.</p><p>The reason for a void pointer is because he&#39;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.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sun, 27 Feb 2011 23:37:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre>peer-&gt;data <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_551.html" target="_blank">malloc</a><span class="k2">(</span>name.size<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_756.html" target="_blank">strcpy</a><span class="k2">(</span>peer-&gt;data, name.c_str<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>

...

cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Peer: "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="k2">(</span><span class="k1">char</span><span class="k3">*</span><span class="k2">)</span>peer-&gt;data <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sun, 27 Feb 2011 23:52:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@ Dustin Dettmer</p><p>That does not work... <br />it gives me : </p><p>&#39;strcpy&#39; : cannot convert parameter 1 from &#39;void *&#39; to &#39;char *&#39;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (alex Ioan)</author>
		<pubDate>Mon, 28 Feb 2011 00:27:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="http://en.wikipedia.org/wiki/Type_conversion">http://en.wikipedia.org/wiki/Type_conversion</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dennis)</author>
		<pubDate>Mon, 28 Feb 2011 00:43:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Denis</p><p>Tried static cast but did not work...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (alex Ioan)</author>
		<pubDate>Mon, 28 Feb 2011 01:08:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It <i>should</i> work:  <a href="http://en.wikipedia.org/wiki/Type_conversion#Explicit_type_conversion_in_C-like_languages">http://en.wikipedia.org/wiki/Type_conversion#Explicit_type_conversion_in_C-like_languages</a>
</p><div class="source-code snippet"><div class="inner"><pre>peer-&gt;data <span class="k3">=</span> <span class="k2">(</span><span class="k1">void</span><span class="k3">*</span><span class="k2">)</span>name.c_str<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Peer-&gt;data: "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="k2">(</span><span class="k1">char</span><span class="k3">*</span><span class="k2">)</span>peer-&gt;data <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dennis)</author>
		<pubDate>Mon, 28 Feb 2011 01:21:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>that worked... thanks ! <br />I was trying<br /> peer-&gt;data = reinterpret_cast&lt;void*&gt;(&amp;name); <br />or <br />peer-&gt;data = reinterpret_cast&lt;void*&gt;(name);<br />and was not giving me anything
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (alex Ioan)</author>
		<pubDate>Mon, 28 Feb 2011 01:29:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well that&#39;s strange, because that should work just fine as well (first version with the &amp;name).</p><p>What do you mean by &quot;was not giving me anything&quot;? If you mean trying to display the data field as a C-string afterwards revealed nothing then I understand.</p><p>Consider the following example:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> <span class="k3">*</span>data <span class="k3">=</span> NULL<span class="k2">;</span>
string name<span class="k2">(</span><span class="s">"Dennis"</span><span class="k2">)</span><span class="k2">;</span>
data <span class="k3">=</span> <span class="k1">reinterpret_cast</span><span class="k3">&lt;</span><span class="k1">void</span><span class="k3">*</span><span class="k3">&gt;</span><span class="k2">(</span><span class="k3">&amp;</span>name<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
This compiles and runs just fine. But outputting data as a C string won&#39;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&#39;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 &quot;string&quot; (not to confuse with the string type from C++) explains why trying to display that reveals nothing).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dennis)</author>
		<pubDate>Mon, 28 Feb 2011 01:51:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre>peer-&gt;data <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_551.html" target="_blank">malloc</a><span class="k2">(</span>name.size<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_756.html" target="_blank">strcpy</a><span class="k2">(</span><span class="k2">(</span><span class="k1">char</span><span class="k3">*</span><span class="k2">)</span>peer-&gt;data, name.c_str<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>

...

cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Peer: "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="k2">(</span><span class="k1">char</span><span class="k3">*</span><span class="k2">)</span>peer-&gt;data <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Mon, 28 Feb 2011 02:25:46 +0000</pubDate>
	</item>
</rss>
