0

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++.

8
  • 4
    Please don't cut of error messages. You left out all the important information that tells you what the problem is! Please add the full error message to your question. Commented Feb 17, 2020 at 15:12
  • 14
    Why are you passing double's to a function that expects vectors of doubles? Voting to close as a typo for passing the wrong data types. Commented Feb 17, 2020 at 15:12
  • 1
    scalar_product is accepting params of type vector but you are passing variables of type double. May be thats a problem! Commented Feb 17, 2020 at 15:14
  • 2
    @mouad2020 scalar_product takes two std::vector<double> const&'s. In hashC, x and y are double, not std::vector<double>. Commented Feb 17, 2020 at 15:17
  • 1
    Sounds like you should be using Rcpp::NumericVector's instead of std::vector's. Commented Feb 17, 2020 at 15:26

1 Answer 1

1

The arguments of scalar_product are both of type std::vector<double> const&, you are passing double type variables, the variables passed as argument must be of compatible type.

Sign up to request clarification or add additional context in comments.

6 Comments

I still have a problem ; i can't use the function hashC within R : in r console , i tried : sourceCpp("C:/Users/mouad/Videos/Private/mouad.cpp")
@mouad2020 this is an interesting question, I would argue you should ask another question reporting this problem with details.
Ok @anastaciu . I will wait some days because there are persons who gives me bad votes for my question . Thank you for your valuable help !
@mouad2020 The DV's are usually related to the lack of compliance with the asking standards of the site, read How do I ask a good question? and also How to create a Minimal, Complete, and Verifiable example, these are quick reads and can help you have better chances of getting UV's.
i found the solution of the second probelm . I should use this prefixe // [[Rcpp::export]] in c++ before each c++ function that i want it to be available within R.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.