Skip to content
Prev 2729 / 10988 Next

[Rcpp-devel] integer arrays as arguments for a module function

I didn't find anything too relevant in the unit tests.  I got a bit stuck,
and asked a question on StackOverflow here:
http://stackoverflow.com/questions/7048888/stdvectorstdstring-to-char-array
.

One answer seemed particularly promising (as shown by the working demo:
http://ideone.com/U6QZ5 ).

However, when I put this code into a module, I get an error:
error: argument of type ?char* (Foo::)(const std::string&)? does not match
?char* (Foo::*)(const std::basic_string<char>&)?

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?

R code:
inc <- paste(readLines('tests/convertCharExample.txt.cpp'),collapse="\n")
fx <- cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" )
a <- Module( "foo_mod", getDynLib(fx) )
b <- new(a$Foo,1:5)
b$convertExample()

convertCharExample.txt.cpp code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstring>

class Foo {
public:
  Foo(IntegerVector tail) {
    this->tail = tail;
  }

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

  int convertExample() {
       std::vector<std::string>  vs;
       vs.push_back("std::string");
       vs.push_back("std::vector<std::string>");
       vs.push_back("char*");
       vs.push_back("std::vector<char*>");
       std::vector<char*>  vc;

       std::transform(vs.begin(), vs.end(), std::back_inserter(vc),
convert);

       for ( size_t i = 0 ; i < vc.size() ; i++ )
            std::cout << vc[i] << std::endl;

       for ( size_t i = 0 ; i < vc.size() ; i++ )
            delete [] vc[i];
       return 0;
  }
private:
  IntegerVector tail;
};

RCPP_MODULE(foo_mod){
using namespace Rcpp ;
 class_<Foo>( "Foo" )
          .constructor<IntegerVector>()
          .method( "convertExample", &Foo::convertExample ,"")
 ;
}
On Fri, Aug 12, 2011 at 5:31 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110813/73ce0698/attachment.htm>