简体   繁体   中英

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. These values are first multiplied with a parameter p_BL that I provide in a parameters list. You can see that at the end of the code I multiply the parameter (from a list) to a integer in a NumericVector . That's where I get the overloaded operator error. How can I resolve this?

Code in the simple_function.cpp file

#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:

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; 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):

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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