小编Tho*_*orf的帖子

从Rcpp中的包装方法返回自定义对象

我对Rcpp模块有以下问题:让我们假设我在Rcpp模块中有两个类

class A {
  public:
    int x;
};

class B
  public:
    A get_an_a(){
      A an_a();
      an_a.x=3;
      return an_a;
    }
};

RCPP_MODULE(mod){
  using namespace Rcpp ;
  class_<A>("A")
   .constructor()
   .property("x",&A::get_x)
  ;
  class_<B>("B)
   .constructor()
   .method("get_an_A",&get_an_a)
  ;
}
Run Code Online (Sandbox Code Playgroud)

.

现在编译失败了,因为它不知道如何处理A的返回类型.

我想我可以用Rcpp :: Xptr做一些事情然而,然后我无法将它连接到Rcpp为类A生成的S4结构.我实际上从R中的方法获得了一个外部指针对象.

是否有可能从第二类的方法中获取正确包装的对象?

谢谢,托马斯

[编辑]

根据Dirk的回答,我构造了一个可以创建包装的S4对象的包装器:

template <> SEXP wrap(const A &obj) { // insprired from "make_new_object" from Rcpp/Module.h
  Rcpp::XPtr<A> xp( new A(obj), true ) ; // copy and mark as finalizable
  Function maker=Environment::Rcpp_namespace()[ "cpp_object_maker"];
  return maker ( typeid(A).name() , xp );
} …
Run Code Online (Sandbox Code Playgroud)

c++ module r rcpp

14
推荐指数
2
解决办法
2288
查看次数

另一个模板(同一类)的模板专业化

我正在写一个数组类.此数组类可以再次包含数组作为成员.实现打印功能时,我需要专业化.

26:template <class T> class array : public vector<T>{
public:
    ...
       string* printToString();
    ...
};
...           
template <class T> string* array<T>::printToString(){
   ...  // generic function
}
template <> inline string* array<double>::printToString(){
   ...  // spezialization for double, works
}
561:template <class U> string* array<array<U>*>::printToString(){
   ...  // does not work
}
Run Code Online (Sandbox Code Playgroud)

最后一个定义产生

src/core/array.h:561: error: invalid use of incomplete type ‘class array<array<T> >’
src/core/array.h:26: error: declaration of ‘class array<array<T> >’
Run Code Online (Sandbox Code Playgroud)

如果重要的话,g ++版本是g ++(Ubuntu 4.4.3-4ubuntu5)4.4.3.有什么想法是什么问题?

托马斯先谢谢你

c++ templates specialization template-specialization

6
推荐指数
1
解决办法
1653
查看次数