这可能是一个基本问题,我一直在努力将用户创建的C++函数传递给Rcpp。我阅读了文档,似乎我应该使用XPtr提供的SEXP包装(链接:http://gallery.rcpp.org/articles/passing-cpp-function-pointers/)但是,我仍然不太清楚如何正确执行此操作。在接下来的内容中,我想使用一个函数funcPtrG作为testfun中的一个参数,C++方式。我得到了如下错误:传递用户在rcpp中创建C++函数作为参数
#include <RcppArmadillo.h>
typedef double (*funcPtrG)(double theta, double gamma);
using namespace Rcpp;
// [[Rcpp::export]]
double GGfuncpp(double theta, double gamma){
double new_gamma = 0;
new_gamma = theta*gamma + R::rnorm(0,1)*0.0001;
return new_gamma;
}
// [[Rcpp::export]]
double testfun(funcPtrG fun2, double theta, double gamma){
double x= 0;
x = fun2(theta,gamma);
return x;
}
Error: cannot convert 'SEXP' to 'double (*)(double, double)' in initialization
我试图把x = XPtr<funcPtr>fun2(theta,gamma)
但没有给出期望的结果。
我愿意!对不起,我会编辑。在我原来的程序中,我有// [[Rcpp :: export]]在所有函数之前 – dleal