2
votes

I am the beginner of Rcpp. Could I ask a very basic question? The following is the simple code:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int test(char d) {
char c;
c=d;
return 0;
}

But when I try to compile it, I always get the error like:

/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include/Rcpp/as.h: In function ‘T   Rcpp::internal::as_string(SEXPREC*, Rcpp::traits::false_type) [with T = char]’:
/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include/Rcpp/as.h:66:   instantiated from ‘T Rcpp::internal::as(SEXPREC*, Rcpp::traits::r_type_string_tag) [with T = char]’
/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include/Rcpp/as.h:126:   instantiated from ‘T Rcpp::as(SEXPREC*) [with T = char]’
test1.cpp:18:   instantiated from here
/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include/Rcpp/as.h:62: error: invalid conversion from ‘const char*’ to ‘char’
make: *** [test1.o] Error 1
g++ -I/usr/local/genomics/programs/R-3.0.0/include -DNDEBUG  -I/usr/local/include  -I"/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include"    -fpic  -g -O2  -c test1.cpp -o test1.o

Error in sourceCpp("test1.cpp") : Error 1 occurred building shared library.

Could you tell me what happens? Thank you very much!

1

1 Answers

2
votes

In some way, that is a bug in the sense that we could support single char objects.

In another way, it does not matter as there is so little useful stuff you can do with a single char. And it works if you do either an int instead

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int mytest(int d) {
  int c;
  c=d;
  return 0;
}

or, better still, use a string type:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int mytest(std::string d) {
  std::string c;
  c=d;
  return 0;
}

And it does of course work when you use Rcpp's own type, CharacterVector.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int mytest(CharacterVector d) {
  CharacterVector c;
  c=d;
  return 0;
}

Single char variables are a bit of an oddity in C and C++ (and you need arrays of them, or pointers) to express "words". So there is no real use in fixing this as R only has vector types anyway.