Note that there are some explanatory texts on larger screens.

plurals
  1. POreturn an array of void*
    text
    copied!<p>I am building a C++ program that needs to store a map of strings to function pointers. However, every function may have different return types and parameters. The way I am attempting to solve this problem is by creating the functions as taking an array of void pointers and returning an array of void pointers, and then casting the arguments and return values as needed.</p> <p>To figure out how this would work, I'm trying to build a simple dummy, but can't get it to compile. I've tried a number of things, but I keep getting different errors. here's an example:</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;map&gt; using namespace std; void** string2map(void** args){ //takes a string of the form "key:value;key:value;..." and returns a map&lt;string,string&gt; string st = *((string**) args)[0]; map&lt;string, string&gt; result = map &lt;string, string&gt;(); //code doesnt matter return (void*) &amp;((void*) &amp;result); } int main(){ string test = "hello:there;how:are you?"; map&lt;string, string&gt; result = *(map&lt;string, string&gt;**)string2map((void*) &amp;((void*) &amp;test))[0]; return 0; } </code></pre> <p>when I try to compile, I get:</p> <pre><code>void.cpp: In function 'void** string2map(void**)': void.cpp:12:34: error: lvalue required as unary '&amp;' operand void.cpp: In function 'int main()': void.cpp:17:89: error: lvalue required as unary '&amp;' operand </code></pre> <p>Obviously there are plenty of things wrong here, but I really just don't know where to start. Can anyone either show me what's wrong with the code above, or give me an alternative to the way I am currently doing it?</p> <p><strong>NOTE</strong></p> <p>The reason I am returning a <code>void**</code> instead of just <code>void*</code> is that there might be a circumstance where I need to return multiple values of different types. An example would be if, above, I wanted to return both the resulting map AND the number of entries in the map. I haven't even gotten to the point of figuring out how to construct that array yet, though.</p> <p><strong>EDIT</strong></p> <p>So based on the responses so far, it seems pretty clear that this is the wrong way of solving this problem. With that in mind, can anyone suggest a better one? I need to be able to store the various function in a single map, which means I need to be able to define a single data type to functions that take and return different types. And it IS important to be able to return multiple values.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload