使用 Rcpp 模块暴露带有引用参数的 C++ 类方法时出错

2024-03-23

我的目标是构建一个数据集类和一个模型类,并将它们都暴露给 R。模型类有一个train()方法引用数据集实例,这似乎是我问题的根源。这是它的样子

//glue.cpp

#include <Rcpp.h>

class MyData
{
public:
  MyData() = default;
};

class MyModel
{
public:
  MyModel() = default;
  void train(const MyData& data) { Rcpp::Rcout << "training model... "; };
};

// Expose MyData
RCPP_MODULE(MyData){
  Rcpp::class_<MyData>("MyData")
  .constructor()
  ;
}

// Expose MyModel
RCPP_MODULE(MyModel){
  Rcpp::class_<MyModel>("MyModel")
  .constructor()
  .method("train", &MyModel::train)
  ;
}

(我应该注意到这个文件,glue.cpp,嵌入在 R 包中。)当我删除这一行时,.method("train", &MyModel::train),我可以通过以下方式编译它而不会出现错误pkgbuild::compile_dll()。有了它,我得到了下面令人讨厌的错误

─  installing *source* package ‘SimpleCppModel’ ...
   ** libs
   clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include  -std=c++14 -fPIC  -Wall -g -O2  -c RcppExports.cpp -o RcppExports.o
   clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include  -std=c++14 -fPIC  -Wall -g -O2  -c glue.cpp -o glue.o
   In file included from glue.cpp:3:
   In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp.h:27:
   In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/RcppCommon.h:168:
   In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/as.h:25:
   /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/internal/Exporter.h:31:28: error: no matching constructor for initialization of 'MyData'
                       Exporter( SEXP x ) : t(x){}
                                            ^ ~
   /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/as.h:87:41: note: in instantiation of member function 'Rcpp::traits::Exporter<MyData>::Exporter' requested here
               ::Rcpp::traits::Exporter<T> exporter(x);
                                           ^
   /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/as.h:152:26: note: in instantiation of function template specialization 'Rcpp::internal::as<MyData>' requested here
           return internal::as<T>(x, typename traits::r_type_traits<T>::r_category());
                            ^
   /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/InputParameter.h:72:54: note: in instantiation of function template specialization 'Rcpp::as<MyData>' requested here
           ConstReferenceInputParameter(SEXP x_) : obj( as<T>(x_) ){}
                                                        ^
   /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:129:58: note: in instantiation of member function 'Rcpp::ConstReferenceInputParameter<MyData>::ConstReferenceInputParameter' requested here
           typename Rcpp::traits::input_parameter<U0>::type x0(args[0]);
                                                            ^
   /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:127:5: note: in instantiation of member function 'Rcpp::CppMethod1<MyModel, void, const MyData &>::operator()' requested here
       CppMethod1( Method m) : method_class(), met(m) {} 
       ^
   /Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/module/Module_generated_method.h:59:27: note: in instantiation of member function 'Rcpp::CppMethod1<MyModel, void, const MyData &>::CppMethod1' requested here
       AddMethod( name_, new CppMethod1<Class,RESULT_TYPE,U0>(fun), valid, docstring);
                             ^
   glue.cpp:30:4: note: in instantiation of function template specialization 'Rcpp::class_<MyModel>::method<void, const MyData &>' requested here
     .method("train", &MyModel::train)
      ^
   glue.cpp:5:7: note: candidate constructor (the implicit copy constructor) not viable: cannot convert argument of incomplete type 'SEXP' (aka 'SEXPREC *') to 'const MyData' for 1st argument
   class MyData
         ^
   glue.cpp:5:7: note: candidate constructor (the implicit move constructor) not viable: cannot convert argument of incomplete type 'SEXP' (aka 'SEXPREC *') to 'MyData' for 1st argument
   glue.cpp:8:3: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
     MyData() = default;
     ^
   1 error generated.
   make: *** [glue.o] Error 1
   ERROR: compilation failed for package ‘SimpleCppModel’
─  removing ‘/private/var/folders/dn/9lp6j6j14t1137ftnnk27wyw0000gn/T/RtmpBqDLoF/devtools_install_4d775ed17ea2/SimpleCppModel’
Error in processx::run(bin, args = real_cmdargs, stdout_line_callback = real_callback(stdout),  : 
  System command error

是什么赋予了?


正如德克在评论中提到的,你需要as<>and wrap专业化MyData。对于您的情况,您可以使用“扩展 Rcpp”小插图中最简单的解决方案:RCPP_EXPOSED_CLASS在包含 Rcpp 中的标头时必须小心:

#include <Rcpp.h>

class MyData
{
public:
  MyData() = default;
};
RCPP_EXPOSED_CLASS(MyData)

class MyModel
{
public:
  MyModel() = default;
  void train(const MyData& data) { Rcpp::Rcout << "training model... " << std::endl; };
};

// Expose MyData
RCPP_MODULE(MyData){
  Rcpp::class_<MyData>("MyData")
  .constructor()
  ;
}

// Expose MyModel
RCPP_MODULE(MyModel){
  Rcpp::class_<MyModel>("MyModel")
  .constructor()
  .method("train", &MyModel::train)
  ;
}

/***R
myData <- new(MyData)
myModel <- new(MyModel)
myModel$train(myData) 
 */

正如您在答案中发现的那样,这甚至有效after包括Rcpp.h。 不过,浏览提到的画廊文章以及小插图仍然有意义。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Rcpp 模块暴露带有引用参数的 C++ 类方法时出错 的相关文章

随机推荐

  • 在 EC2 区域中传播 MongoDB

    我想在多个 Amazon EC2 区域中分发分片 复制的 MongoDB 设置 此流量是否已由 MongoDB 加密 或者我可以选择进行设置吗 或者亚马逊是否在其数据中心之间提供类似 VPN 的特殊连接 我昨天回答了一个关于 Apache
  • Rails UTF-8 响应

    我有一个在 Ruby 1 9 3 上运行的 Rails 3 2 应用程序 它返回存储在 MongoDB 数据库中的 JSON 数据 数据似乎正确存储在 mongo 中 例如 看name属性 id ObjectId 4f986cbe4c808
  • 在 Phantomjs + selenium 中启用 cookies

    我想登录amazons3 网址 https console aws amazon com iam home security credential https console aws amazon com iam home security
  • EF 代码优先迁移,DB 生成 GUID 密钥

    这个问题是单行的 如何在代码优先迁移中生成 EF ALTER TABLE DMS Forms ADD CONSTRAINT DF DMS Forms Id DEFAULT newid FOR Id GO 问题的更多细节 我要构建一个简单的表
  • 嵌套弹性框下方的“魔法”空白(不是填充或边距)

    Note Original title was White space below MUI Grid container which is not a padding or margin but this is not related to
  • 来自同一数据集的多个 ComboBox 控件

    我在 Windows 窗体上有 2 个 DropDownList 组合框 它们都从同一数据集 人员列表 填充 但它们具有不同的用途 项目经理 审阅者 如果我将它们的数据源都设置为数据集 它们都会绑定到数据集并同时更改 我是否遗漏了某些内容
  • 如何计算 RandomForestRegression 中的 MSE 标准?

    我现在使用 sklearn ensemble 中的 RandomForestRegressor 来分析数据集 并选择 mse 作为衡量分割质量的函数 但我不太清楚mse是如何计算的 有人可以在这里向我解释一下 用方程更好 或者为我提供一些参
  • UITableView deleteRowsAtIndexPaths 不删除行

    我在IB中设计了UIView 并在其上设计了UITableView 在视图控制器的 viewDidLoad 方法中 我初始化自定义 UITableViewContoller 并设置 dataSource 和委托 void viewDidLo
  • CSS 拉伸内容容器高度,如果空则太大则溢出[重复]

    这个问题在这里已经有答案了 我所拥有的是一个简单的结构container接下来是两个子元素 contentand footer footer有固定的高度并且content应填充剩余的空白空间 这很容易实现display table 但由于某
  • 获取 Facebook 个人资料图片

    我需要通过传递 ID 来获取任何人的 Facebook 个人资料图片 但我不需要使用 facebook API 或 Graph 任何其他东西 只是我需要提供带有该 id 的 url 是否可以通过这种方式获取个人资料图片 我在谷歌尝试过 但没
  • 根据条件添加或删除类

    我正在从元素的类中添加 删除类classList基于变量的真实性 然而 我这样做的方式似乎是一种迟钝的方式 if myConditionIsMet myEl classList add myClass else myEl classList
  • [Authorize] 失败后显示 404 错误页面

    我有一个操作想限制为仅角色 管理员 我是这样做的 Authorize Roles Admin public ActionResult Edit int id 手动进入 Controller Edit 1 路径后 我被重定向到登录页面 好吧
  • 如何在 PHP 应用程序中打印 mysql 中的行号

    我有一个 mysql 表需要在前端应用程序中显示数据以及行号 以下查询在 phpMyadmin 中完美运行 SET row num 0 SELECT row num row num 1 AS num INV DOS PTNAME BAL P
  • 为什么 Jenkins 中的 ClearCase UCM 插件无法找到任何基线?

    我正在尝试设置 Jenkins v1 47 来使用ClearCase UCM v1 1 2 插件 https wiki jenkins ci org display JENKINS ClearCase UCM Plugin 使用以下配置 名
  • 在 C++ 中放置新的派生基子对象

    是否定义了放置新派生类的可简单破坏的基础对象的行为 struct base int ref struct derived public base complicated object complicated derived int r co
  • 如何将数据附加到 csv 文件

    我想将数据附加到 csv 文件 我使用 html 表单从用户那里获取数据 我当前的代码将数据添加到 csv 文件 但不附加 因此 当用户输入详细信息时 请点击注册按钮 然后它会删除之前添加的数据 并将新数据插入到该位置 那么如何附加新数据以
  • 是否可以在 Mavericks 上创建与 Xcode 5.0.2 兼容的 iOS 4 - iOS 7 应用程序?

    我需要开发一个支持 iOS 4 iOS 7 的应用程序 是否可以在 Mavericks 上的 XCode 5 0 2 上实现 xcode 为我提供的最低部署目标是 iOS 6 在项目 gt 目标 gt 构建设置 gt 架构中 改变架构 fr
  • 无法卸载需求jupyter,未安装

    我已经安装了jupyter但想卸载它 然而这是不可能的 pip freeze grep jupyter pip3 freeze grep jupyter jupyter client 5 1 0 jupyter console 5 2 0
  • 为什么 location.reload() 比其他页面重新加载方法慢?

    几个月前我发帖这个答案 https stackoverflow com a 17259514 1420197关于如何通过 JavaScript 刷新页面 我提供了一个JSFiddle演示 http jsfiddle net hE6Ua to
  • 使用 Rcpp 模块暴露带有引用参数的 C++ 类方法时出错

    我的目标是构建一个数据集类和一个模型类 并将它们都暴露给 R 模型类有一个train 方法引用数据集实例 这似乎是我问题的根源 这是它的样子 glue cpp include