I'm trying to developp some c++ functions in order to reuse them within my R project.
#include <Rcpp.h> using namespace Rcpp; double scalar_product(std::vector<double> const& a, std::vector<double> const& b) { if (a.size() != b.size()) { throw std::runtime_error("different sizes"); } return std::inner_product(a.begin(), a.end(), b.begin(), 0.0); } // scalar_product double hashC(double x, double y) { double ys = scalar_product(x , y); if (ys > 0) { return 1; } else if (ys == 0) { return 0; } else { return 0; } } /*** R x=c(1,2,3,4) y=c(-1,-2,-3,-5) d=hashC(x,y) print("d") print(d) */ I got an error : invalid initialization of reference of type 'const std :: ......
The error is shown within that line :
double ys = scalar_product(x , y); Sorry but i'm not good at c++.
double's to a function that expects vectors of doubles? Voting to close as a typo for passing the wrong data types.scalar_producttakes twostd::vector<double> const&'s. InhashC,xandyaredouble, notstd::vector<double>.Rcpp::NumericVector's instead ofstd::vector's.