简体   繁体   English

在 Rcpp 中使用列表和数字向量时如何避免歧义重载运算符问题?

[英]How to avoid the ambiguous overloaded operator problem when using list and numeric vector in Rcpp?

Goal目标

The code below is a very simplified version of my original code for creating a reproducible example.下面的代码是我用于创建可重现示例的原始代码的非常简化的版本。 In this code, I am trying to generate a vector that is filled up by using 2 values in each iteration from a for-loop.在这段代码中,我试图生成一个向量,该向量通过在 for 循环的每次迭代中使用 2 个值来填充。 These values are first multiplied with a parameter p_BL that I provide in a parameters list.这些值首先与我在parameters列表中提供的参数p_BL相乘。 You can see that at the end of the code I multiply the parameter (from a list) to a integer in a NumericVector .您可以看到,在代码的末尾,我将参数(来自列表)乘以一个NumericVector的整数。 That's where I get the overloaded operator error.这就是我得到重载运算符错误的地方。 How can I resolve this?我该如何解决这个问题?

Code in the simple_function.cpp file simple_function.cpp文件中的代码

#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]
NumericVector my_func(List parameters, int number_simulations)
{

  NumericVector vector_trig_times (number_simulations);
  NumericVector vector_trig_times1 (number_simulations);
  NumericVector vector_trig_times2 (number_simulations);


  for (int i_simulation = 0; i_simulation < number_simulations; i_simulation++) {

    int vector_activation1 = i_simulation + 2;

    int vector_activation2 = i_simulation + 5;



    vector_trig_times1[i_simulation] = vector_activation1;

    vector_trig_times2[i_simulation] = vector_activation2;



    vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

  } // i_simulations for loop


    return vector_trig_times;
}

Compiling the file:编译文件:

library(Rcpp)
sourceCpp("simple_function.cpp")

Error错误

在此处输入图片说明

Note: Line 28 refers to:注:第 28 行是指:

vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

Edit: Text of the error message编辑:错误消息的文本

C:/RBuildTools/3.5/mingw_64/bin/g++  -I"C:/PROGRA~1/R/R-36~1.1/include" -DNDEBUG   -I"C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include" -I"C:/Users/durraniu/GOOGLE~1/DISSER~1"        -O2 -Wall  -mtune=generic -c simple_function.cpp -o simple_function.o

simple_function.cpp: In function 'Rcpp::NumericVector my_func(Rcpp::List, int)':

simple_function.cpp:28:59: error: no match for 'operator*' (operand types are 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' and 'Rcpp::traits::storage_type<14>::type {aka double}')

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                           ^

simple_function.cpp:28:59: note: candidates are:

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/RcppCommon.h:136:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:27,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/complex.h:25:17: note: Rcomplex operator*(const Rcomplex&, const Rcomplex&)

 inline Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs) {

                 ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/complex.h:25:17: note:   no known conversion for argument 2 from 'Rcpp::traits::storage_type<14>::type {aka double}' to 'const Rcomplex&'

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:473:1: note: template<int RTYPE, bool LHS_NA, class LHS_T, bool RHS_NA, class RHS_T> Rcpp::sugar::Times_Vector_Vector<RTYPE, LHS_NA, LHS_T, RHS_NA, RHS_T> Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const Rcpp::VectorBase<RTYPE, RHS_NA, RHS_T>&)

 operator*(

 ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:473:1: note:   template argument deduction/substitution failed:

simple_function.cpp:28:92: note:   'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>'

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                                                            ^

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:459:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive_nona<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::sugar::NonaPrimitive<U>&, const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&)

 operator*(

 ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:459:1: note:   template argument deduction/substitution failed:

simple_function.cpp:28:92: note:   'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::sugar::NonaPrimitive<U>'

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                                                            ^

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:450:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive_nona<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const Rcpp::sugar::NonaPrimitive<U>&)

 operator*(

 ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:450:1: note:   template argument deduction/substitution failed:

simple_function.cpp:28:92: note:   'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>'

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                                                            ^

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:439:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive<RTYPE, NA, T> >::type Rcpp::operator*(const U&, const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&)

 operator*(

 ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:439:1: note:   template argument deduction/substitution failed:

simple_function.cpp:28:92: note:   mismatched types 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>' and 'Rcpp::traits::storage_type<14>::type {aka double}'

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                                                            ^

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:429:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const U&)

 operator*(

 ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:429:1: note:   template argument deduction/substitution failed:

simple_function.cpp:28:92: note:   'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>'

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                                                            ^

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/Vector.h:58:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:40,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:271:14: note: template<int RTYPE, template<class> class StoragePolicy, class T> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<T>::type, typename Rcpp::Matrix<RTYPE, StoragePolicy>::stored_type>::value, Rcpp::Matrix<RTYPE, StoragePolicy> >::type Rcpp::operator*(const T&, const Rcpp::Matrix<RTYPE, StoragePolicy>&)

              operator __OPERATOR__ (const T &lhs, const Matrix<RTYPE, StoragePolicy> &rhs) {                                  \

              ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:280:1: note: in expansion of macro 'RCPP_GENERATE_SCALAR_MATRIX_OPERATOR'

 RCPP_GENERATE_SCALAR_MATRIX_OPERATOR(*)

 ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:271:14: note:   template argument deduction/substitution failed:

              operator __OPERATOR__ (const T &lhs, const Matrix<RTYPE, StoragePolicy> &rhs) {                                  \

              ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:280:1: note: in expansion of macro 'RCPP_GENERATE_SCALAR_MATRIX_OPERATOR'

 RCPP_GENERATE_SCALAR_MATRIX_OPERATOR(*)

 ^

simple_function.cpp:28:92: note:   mismatched types 'const Rcpp::Matrix<RTYPE, StoragePolicy>' and 'Rcpp::traits::storage_type<14>::type {aka double}'

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                                                            ^

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/Vector.h:58:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:40,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:254:14: note: template<int RTYPE, template<class> class StoragePolicy, class T> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<T>::type, typename Rcpp::Matrix<RTYPE, StoragePolicy>::stored_type>::value, Rcpp::Matrix<RTYPE, StoragePolicy> >::type Rcpp::operator*(const Rcpp::Matrix<RTYPE, StoragePolicy>&, const T&)

              operator __OPERATOR__ (const Matrix<RTYPE, StoragePolicy> &lhs, const T &rhs) {                                  \

              ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:262:1: note: in expansion of macro 'RCPP_GENERATE_MATRIX_SCALAR_OPERATOR'

 RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(*)

 ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:254:14: note:   template argument deduction/substitution failed:

              operator __OPERATOR__ (const Matrix<RTYPE, StoragePolicy> &lhs, const T &rhs) {                                  \

              ^

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:262:1: note: in expansion of macro 'RCPP_GENERATE_MATRIX_SCALAR_OPERATOR'

 RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(*)

 ^

simple_function.cpp:28:92: note:   'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::Matrix<RTYPE, StoragePolicy>'

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                                                            ^

simple_function.cpp:28:100: error: ambiguous overload for 'operator-' (operand types are 'int' and 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}')

     vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);

                                                                                                    ^

simple_function.cpp:28:100: note: candidates are:

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/date_datetime.h:29:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:62,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/Datetime.h:158:20: note: double Rcpp::operator-(const Rcpp::Datetime&, const Rcpp::Datetime&)

     inline double  operator-(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt; }

                    ^

In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/date_datetime.h:25:0,

                 from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:62,

                 from simple_function.cpp:1:

C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/Date.h:164:19: note: double Rcpp::operator-(const Rcpp::Date&, const Rcpp::Date&)

     inline double operator-( const Date& d1, const Date& d2) { return d1.m_d -  d2.m_d; }

                   ^

make: *** [C:/PROGRA~1/R/R-36~1.1/etc/x64/Makeconf:215: simple_function.o] Error 1

Error in sourceCpp("simple_function.cpp") : 
  Error 1 occurred building shared library.

Here's how you could implement the spot-on suggestions of Roland and Ralf;以下是如何实施 Roland 和 Ralf 的即时建议; notice I've also cut out the unnecessary vector creation and assignment for your i+2 and i+5 values (I also used i as a counter because I don't think i_simulation is more informative, so it makes your code less readable as the line either has to go way over 80 characters or you have to split the calculation over two lines):请注意,我还为您的i+2i+5值删除了不必要的向量创建和分配(我也将i用作计数器,因为我认为i_simulation没有提供更多信息,因此它使您的代码可读性降低,因为该行要么必须超过 80 个字符,要么必须将计算拆分为两行):

#include <Rcpp.h>

// [[Rcpp::export]]
NumericVector my_func2(List parameters, int number_simulations)
{

    NumericVector vector_trig_times (number_simulations);
    double p_BL = as<NumericVector>(parameters["p_BL"])[0];

    for ( int i = 0; i < number_simulations; ++i ) {
        vector_trig_times[i] = (p_BL * (i + 5)) + ((1 - p_BL) * (i + 2));
    }

    return vector_trig_times;
}

This compiles just fine, and the result is as expected:这编译得很好,结果如预期:

Rcpp::sourceCpp("so.cpp")
my_func2(list(p_BL = 0.5), 10)
# [1]  3.5  4.5  5.5  6.5  7.5  8.5  9.5 10.5 11.5 12.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM