简体   繁体   中英

Rcpp compile error: cannot convert List to SEXP

If I adapt some working Rcpp code to put it into a class, it stops working.

Here is the working, non-class-based code:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
List test1_() {
  Rcpp::NumericVector x = Rcpp::NumericVector::create(1.0);
  return Rcpp::List::create(Rcpp::Named("x") = x);
}

But when I separate it into .cpp ...

// testlist.cpp
#include <Rcpp.h>
#include "testlist.h"
using namespace Rcpp;

// [[Rcpp::export]]
SEXP test2_() {
  testlist a_testlist;
  return a_testlist.test;
}

... and .h ...

// testlist.h
#ifndef TESTLIST_
#define TESTLIST_

#include <Rcpp.h>

class testlist {

public:

  testlist() {}

  Rcpp::List test() {
    Rcpp::NumericVector x = Rcpp::NumericVector::create(1.0);
    return Rcpp::List::create(Rcpp::Named("x") = x);
  }

};

#endif

... then I get the following compilation error.

g++ -I/usr/include/R/ -DNDEBUG  -D_FORTIFY_SOURCE=2 -I"/home/nacnudus/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include"   -fpic  -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong  -c RcppExports.cpp -o RcppExports.o
g++ -I/usr/include/R/ -DNDEBUG  -D_FORTIFY_SOURCE=2 -I"/home/nacnudus/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include"   -fpic  -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong  -c testlist.cpp -o testlist.o
testlist.cpp: In function ‘Rcpp::List test2_()’:
testlist.cpp:8:21: error: cannot convert ‘testlist::test’ from type ‘Rcpp::List (testlist::)() {aka Rcpp::Vector<19> (testlist::)()}’ to type ‘Rcpp::List {aka Rcpp::Vector<19>}’
   return a_testlist.test;
                     ^~~~
make: *** [/usr/lib64/R/etc/Makeconf:141: testlist.o] Error 1
ERROR: compilation failed for package ‘testlist’
* removing ‘/tmp/RtmppNxPq5/devtools_install_f654fb60a32/testlist’

What am I doing wrong?

Simple typo:

return a_testlist.test;

Needs to be:

return a_testlist.test();

Though, you should consider reading the Rcpp-modules vignette to properly expose classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM