<div>I didn'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&)’ does not match ‘char* (Foo::*)(const std::basic_string<char>&)’</div>
<div><br></div><div>Here's the example I'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 <- paste(readLines('tests/convertCharExample.txt.cpp'),collapse="\n")</div><div>fx <- cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" )</div><div>a <- Module( "foo_mod", getDynLib(fx) )</div>
<div>b <- new(a$Foo,1:5)</div><div>b$convertExample()</div></div><div><br></div><div>convertCharExample.txt.cpp code:</div><div><div>#include <iostream></div><div>#include <string></div><div>#include <vector></div>
<div>#include <algorithm></div><div>#include <iterator></div><div>#include <cstring></div><div> </div><div>class Foo {</div><div>public:</div><div> Foo(IntegerVector tail) {</div><div> this->tail = tail;</div>
<div> }</div><div><br></div><div> char *convert(const std::string & 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<std::string> vs;</div><div> vs.push_back("std::string");</div><div> vs.push_back("std::vector<std::string>");</div>
<div> vs.push_back("char*");</div><div> vs.push_back("std::vector<char*>");</div><div> std::vector<char*> 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 < vc.size() ; i++ )</div><div> std::cout << vc[i] << std::endl;</div><div><br></div><div> for ( size_t i = 0 ; i < 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_<Foo>( "Foo" )</div><div> .constructor<IntegerVector>() </div><div> .method( "convertExample", &Foo::convertExample ,"")</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"><<a href="mailto:edd@debian.org">edd@debian.org</a>></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'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<16>::iterator’ to ‘char*’ in<br>
| assignment<br>
<br>
</div>char can be a pain. It is something C and C++ didn'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< std::string >. 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 <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br>
|<br>
|<br>
| On 12 August 2011 at 14:50, Chris DuBois wrote:<br>
| | Hi all,<br>
| |<br>
| | I'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't compile (though I'm unfamiliar with C++ in<br>
| | general, so maybe this shouldn'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, <int>, 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>