<div>I didn&#39;t find anything too relevant in the unit tests.  I got a bit stuck, and asked a question on StackOverflow here: <a href="http://stackoverflow.com/questions/7048888/stdvectorstdstring-to-char-array">http://stackoverflow.com/questions/7048888/stdvectorstdstring-to-char-array</a>. </div>

<div> </div><div>One answer seemed particularly promising (as shown by the working demo:<meta http-equiv="content-type" content="text/html; charset=utf-8"><a href="http://ideone.com/U6QZ5">http://ideone.com/U6QZ5</a> ).<div>

<br></div><div>However, when I put this code into a module, I get an error:</div><div>error: argument of type ‘char* (Foo::)(const std::string&amp;)’ does not match ‘char* (Foo::*)(const std::basic_string&lt;char&gt;&amp;)’</div>

<div><br></div><div>Here&#39;s the example I&#39;m working with.  Commenting out the std::transform and it compiles fine.  Why does it work OK when not in a module, but fails when in a module?</div><div><br></div><div>R code:</div>

<div><div>inc &lt;- paste(readLines(&#39;tests/convertCharExample.txt.cpp&#39;),collapse=&quot;\n&quot;)</div><div>fx &lt;- cxxfunction( signature(), &quot;&quot; , include = inc, plugin = &quot;Rcpp&quot; )</div><div>a &lt;- Module( &quot;foo_mod&quot;, getDynLib(fx) )</div>

<div>b &lt;- new(a$Foo,1:5)</div><div>b$convertExample()</div></div><div><br></div><div>convertCharExample.txt.cpp code:</div><div><div>#include &lt;iostream&gt;</div><div>#include &lt;string&gt;</div><div>#include &lt;vector&gt;</div>

<div>#include &lt;algorithm&gt;</div><div>#include &lt;iterator&gt;</div><div>#include &lt;cstring&gt;</div><div> </div><div>class Foo {</div><div>public:</div><div>  Foo(IntegerVector tail) {</div><div>    this-&gt;tail = tail;</div>

<div>  }</div><div><br></div><div>  char *convert(const std::string &amp; s)</div><div>  {</div><div>    char *pc = new char[s.size()+1];</div><div>    std::strcpy(pc, s.c_str());</div><div>    return pc; </div><div>  }</div>

<div><br></div><div>  int convertExample() {</div><div>       std::vector&lt;std::string&gt;  vs;</div><div>       vs.push_back(&quot;std::string&quot;);</div><div>       vs.push_back(&quot;std::vector&lt;std::string&gt;&quot;);</div>

<div>       vs.push_back(&quot;char*&quot;);</div><div>       vs.push_back(&quot;std::vector&lt;char*&gt;&quot;);</div><div>       std::vector&lt;char*&gt;  vc;</div><div><br></div><div>       std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);<span class="Apple-tab-span" style="white-space:pre">        </span></div>

<div><br></div><div>       for ( size_t i = 0 ; i &lt; vc.size() ; i++ )</div><div>            std::cout &lt;&lt; vc[i] &lt;&lt; std::endl;</div><div><br></div><div>       for ( size_t i = 0 ; i &lt; vc.size() ; i++ )</div>

<div>            delete [] vc[i];</div><div>       return 0;</div><div>  }</div><div>private:</div><div>  IntegerVector tail;</div><div>};</div><div><br></div><div>RCPP_MODULE(foo_mod){</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>using namespace Rcpp ;</div>

<div><span class="Apple-tab-span" style="white-space:pre">        </span>class_&lt;Foo&gt;( &quot;Foo&quot; )</div><div>          .constructor&lt;IntegerVector&gt;()    </div><div>          .method( &quot;convertExample&quot;, &amp;Foo::convertExample ,&quot;&quot;)</div>

<div><span class="Apple-tab-span" style="white-space:pre">        </span>;</div><div>}                     </div><div><br></div><br><div class="gmail_quote">On Fri, Aug 12, 2011 at 5:31 PM, Dirk Eddelbuettel <span dir="ltr">&lt;<a href="mailto:edd@debian.org">edd@debian.org</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><br>
On 12 August 2011 at 16:59, Chris DuBois wrote:<br>
| Point taken: STL looks like the way to go in general.  <br>
|<br>
| In my particular example, however, the arrays get immediately cast to another<br>
| structure foo (somebody else&#39;s code).  So I need to cast from IntegerVector to<br>
| int[] before they get cast to foo[].  What you suggested works perfectly (and<br>
| makes sense in hindsight).<br>
|<br>
| Is the story identical for char[]?  Where x is a CharacterVector, I tried <br>
|<br>
| char* a = x.begin();<br>
|<br>
| and got<br>
| error: cannot convert ‘Rcpp::Vector&lt;16&gt;::iterator’ to ‘char*’ in<br>
| assignment<br>
<br>
</div>char can be a pain. It is something C and C++ didn&#39;t get quite right by<br>
lacking a base type for string.  Doing it in plain C (as in *argv[] from<br>
main()) is a pain but doable.<br>
<br>
CharacterVector works as the equivalent of std::vector&lt; std::string &gt;. Out of<br>
each element (ie string) you can extract the underlying char* but you may<br>
have to rely on strcmp etc.  Remember that you may be dealing with pointers<br>
of pointers...<br>
<br>
Have a peek at the unitTests/ directory to see if you find something.<br>
<br>
Hope this helps,  Dirk<br>
<div><div></div><div class="h5"><br>
| Any help much appreciated.<br>
| Chris<br>
|<br>
| On Fri, Aug 12, 2011 at 3:19 PM, Dirk Eddelbuettel &lt;<a href="mailto:edd@debian.org">edd@debian.org</a>&gt; wrote:<br>
|<br>
|<br>
|     On 12 August 2011 at 14:50, Chris DuBois wrote:<br>
|     | Hi all,<br>
|     |<br>
|     | I&#39;m trying to figure out how to pass in an array of integers to a<br>
|     function<br>
|     | inside a module.  For example, adding the following function to<br>
|     runit.Module.R<br>
|     | works fine:<br>
|     |<br>
|     |         int bla3( IntegerVector x ) {<br>
|     |               return sum(x);<br>
|     |         }<br>
|     |<br>
|     | However, I need to pass an int array, rather than an IntegerVector.<br>
|      Using int<br>
|     | x[] in the arguments doesn&#39;t compile (though I&#39;m unfamiliar with C++ in<br>
|     | general, so maybe this shouldn&#39;t work anyway).  <br>
|     |<br>
|     | Alternatively, should I just cast x from an IntegerVector to an int<br>
|     array?  I<br>
|     | tried various permutations of as, vector, &lt;int&gt;, etc, and would like to<br>
|     learn<br>
|     | the proper way of doing this.<br>
|<br>
|     You generally do not want old school x[] arrays in C++. Why?  Because STL<br>
|     vectors do _everything_ they do at (essentially) zero added cost, free you<br>
|     from malloc/free and still allow you to access the straight memory should<br>
|     you<br>
|     need to (to talk to a C API, say).<br>
|<br>
|     So use IntegerVector for _the interface_. You can the, if you must, do<br>
|<br>
|         IntegerVector x;<br>
|<br>
|         int a1[] = x.begin();     // STL-style iterator to beginning of<br>
|     memory<br>
|         int *a2  = x.begin();     // idem<br>
|<br>
|     Hope this helps,  Dirk<br>
|<br>
|     --<br>
|     Two new Rcpp master classes for R and C++ integration scheduled for<br>
|     New York (Sep 24) and San Francisco (Oct 8), more details are at<br>
|     <a href="http://dirk.eddelbuettel.com/blog/2011/08/04#" target="_blank">http://dirk.eddelbuettel.com/blog/2011/08/04#</a><br>
|     rcpp_classes_2011-09_and_2011-10<br>
|     <a href="http://www.revolutionanalytics.com/products/training/public/" target="_blank">http://www.revolutionanalytics.com/products/training/public/</a><br>
|     rcpp-master-class.php<br>
|<br>
|<br>
<br>
</div></div>--<br>
<div><div></div><div class="h5">Two new Rcpp master classes for R and C++ integration scheduled for<br>
New York (Sep 24) and San Francisco (Oct 8), more details are at<br>
<a href="http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10" target="_blank">http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10</a><br>
<a href="http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php" target="_blank">http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php</a><br>
</div></div></blockquote></div><br></div></div>