具有 Rcpp::Function 成员的 C++ 类

标签 c++ r rcpp

使用 Rcpp,我希望能够创建一个具有字段 Rcpp::Function 的 C++ 类。例如:

class myClass{
    Rcpp::Function myR_fun;
    myClass(Rcpp::Function userR_fun){
        myR_fun = userR_fun;
    }
};

不幸的是,上面的代码不起作用。编译时报如下错误:

error: constructor for 'myClass' must explicitly initialize the member 'myR_fun'
which does not have a default constructor
    myClass(Rcpp::Function userR_fun){
    ^

错误报告有点令人困惑,因为我认为我已经在 myClass 的构造函数中初始化了 myR_fun

我可以使用的解决方法是使用一个空指针

class myClass{
    void* vFunPtr;
    myClass(Rcpp::Function userR_fun){
        vFunPtr = &userR_fun;
    }
};

但从组织的角度来看,这似乎不是最理想的。使 Rcpp::Function 对象成为 C++ 类的字段的正确方法是什么?

最佳答案

根据您的语法,首先默认构造myR_fun,然后分配userR_fun

试试这个:

class myClass {
    Rcpp::Function myR_fun;
    myClass(Rcpp::Function userR_fun)
        : myR_fun(userR_fun)
    {}
};

通过此语法,myR_fun 是使用 userR_fun 直接构造的。

关于具有 Rcpp::Function 成员的 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39325189/

相关文章:

c++ - 使用-static-libstdc++ 构建应用程序和共享库时出现段错误

c++ - 无效的堆参数 : allocated with operator new[], 通过运算符(operator)删除释放

c++ - 符合标准的 C++ 实现如何表明它不知道当前日期和时间?

R,使用 load() 从 .rda 对象分配内容

RStudio 与具有可重现代码的 RCpp 一起崩溃

xcode - 内联和 Xcode 4.2.1 出错

c++ - 使用声明和访问修饰符的嵌套类

r - 从 xts 中获取每天前 5 分钟的数据或对其进行子集化

r - 为什么 TRUE == "TRUE"在 R 中是 TRUE?

c++ - Rcpp:我的距离矩阵程序比包中的函数慢