Rcpp compile error: cannot convert List to SEXP

Rcpp compile error: cannot convert List to SEXP

如果我修改一些工作的 Rcpp 代码以将其放入 class,它将停止工作。

这是工作的、非 class 的代码:

#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);
}

但是当我把它分成 .cpp 时...

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

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

...和.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

...然后我得到以下编译错误。

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’

我做错了什么?

简单的错字:

return a_testlist.test;

需要:

return a_testlist.test();

不过,您应该考虑阅读 Rcpp-modules 插图以正确公开 类。