【问题标题】:Rcpp compile error: cannot convert List to SEXPRcpp 编译错误:无法将 List 转换为 SEXP
【发布时间】:2016-11-09 00:48:54
【问题描述】:

如果我调整一些工作的 Rcpp 代码将其放入一个类中,它就会停止工作。

这是工作的、非基于类的代码:

#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’

我做错了什么?

【问题讨论】:

    标签: r rcpp


    【解决方案1】:

    简单的错字:

    return a_testlist.test;
    

    需要:

    return a_testlist.test();
    

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

    【讨论】:

    • 那行得通,但只有在我擦除目录并重新开始之后。因为在这个问题中遇到问题,我省略了():stackoverflow.com/a/877538/937932,但也许这只是为了声明类的实例,而不是为了调用方法。
    • 我阅读了小插图,但没有看到在这种情况下必须添加什么,因为我已经在使用 // [[Rcpp::export]]。似乎更多的是直接对 R 的整个类,这绝对不是我想要做的,还是我误解了?
    • 评论 1: Foo 是一个新的类初始化,而这里是一个被调用的方法。 注释 2: modules 组件是关于在 R 中提供整个类。如果您不需要它作为一个特性,那么只需继续当前的类声明和第二个方法导出。不过这可能更容易出错。
    猜你喜欢
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2018-08-09
    • 2020-12-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多