RCPP_MODULE 用于继承类 [英] RCPP_MODULE for inheritance class

查看:25
本文介绍了RCPP_MODULE 用于继承类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用继承类的模块时遇到问题.基本上,我无法编译这部分.

I got a problem when I try module with inheritance class. Basiclly, I can't compile this part.

RCPP_MODULE(PACE){
    using namespace Rcpp;
    class_<FPCAreg>("FPCAreg")
        .constructor<List, List, double, double, int, bool, bool>()
        .field("n", &FPCAreg::n)
            ;
}

这里的FPCAreg是基于FPCA类的继承类,在FPCA中定义了n".无论我使用".field("n", &FPCAreg::n)" 还是".field("n", &FPCA::n)".当我使用 ".field("n", &FPCAreg::n)" 时,错误消息是"没有匹配的函数调用 blablabla",对于 ".field("n", &FPCA::n)",这是相同但不同的类名.有什么建议吗?谢谢.

Here FPCAreg is a inheritance class based on FPCA class, and "n" is defined in FPCA. No matter I use ".field("n", &FPCAreg::n)" or ".field("n", &FPCA::n)". When I use ".field("n", &FPCAreg::n)", the error message is "no matching function for call to blablabla", and for ".field("n", &FPCA::n)", that's same but different class name. Is there any suggestion? Thanks.

推荐答案

您只能从实际的类中声明字段和方法.但是,你可以做的是暴露基类和派生类,并在derives中暗示它们之间的继承关系.

You can only declare fields and methods from the actual class. However, what you can do is expose the base class and the derived class, and hint the inheritance between them in derives.

这是一个例子:

#include <Rcpp.h>
using namespace Rcpp;

class Base {
public:
    Base(double x_) : x(x_){}
    double x; 
} ;

class Derived : public Base {
public:
    Derived(int y_) : Base(2.0), y(y_){} 
    int y ;
} ;

RCPP_MODULE(PACE){
    class_<Base>("Base")
        .constructor<double>()
        .field("x", &Base::x) 
    ;
    class_<Derived>("Derived")
        .derives<Base>("Base")
        .constructor<int>()
        .field("y", &Derived::y)
    ;
}

然后我从 R 得到这个:

I get this then from R:

> obj <- new(Derived, 10L)
> obj$y
[1] 10
> obj$x
[1] 2

这篇关于RCPP_MODULE 用于继承类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆