Using a c++ function in an other c++ function (inside a R Package with Rcpp) -


i try create r package (using rcpp). works fine. wrote c++ function, want call in other c++ file. in /src there is:

  • function1 = linearinterpolation.cpp
  • function2 = getslope.cpp

let's take function1, linear interpolation between 2 points @ specific position.

#include <rcpp.h>  using namespace rcpp;  //' @name linearinterpolation //' @title linearinterpolation //' @description twodimensional linearinterpolation specific point //' @param xcoordinates 2 x coordinates //' @param ycoordinates coresponding 2 y coordinates //' @param atposition point interpolation shall done //' @return returns linear interpolated y-value specific point //' @examples //' linearinterpolation(c(1,2),c(1,4),3) //' //' @export // [[rcpp::export]] double linearinterpolation(numericvector xcoordinates, numericvector ycoordinates, double atposition) {    // start + delta y / delta x_1 * delta x_2   return ycoordinates[1] + getslope(xcoordinates, ycoordinates) * (atposition - xcoordinates[1]);  } 

and slope calculated in different function (file).

#include <rcpp.h>  using namespace rcpp;  //' @name getslope //' @title getslope //' @description calculates slopes between 2 points in 2dimensions //' @param xcoordinates 2 x coordinates //' @param ycoordinates coresponding 2 y coordinates //' @return returns slope //' @examples //' getslope(c(1,2),c(1,4),3) //' //' @export // [[rcpp::export]] double getslope(numericvector xcoordinates, numericvector ycoordinates) {   return (ycoordinates[1] - ycoordinates[0]) / (xcoordinates[1] - xcoordinates[0]); } 

i not have deeper knowledge in rcpp or c++. read vignette , writing package uses rcpp think read right parts didn't it.

why getslope function not "visible" in other function - both in same package. how can use getslope in other file?

sorry stuck.

thanks , best regards

nico

perhaps should make file, header file .hpp, , put in it:

#include <rcpp.h> using namespace rcpp; double getslope(numericvector xcoordinates, numericvector ycoordinates); 

or, better still,

#include <rcpp.h> double getslope(rcpp:: numericvector xcoordinates, rcpp:: numericvector ycoordinates); 

and put #include"myheader.hpp" @ top of both of cpp files. in order declare function such both cpp files can see it.


... both in same package

just because 2 things in same r package, not mean in same c++ translation unit (i.e. cpp file), , translation unit important thing 2 c++ functions see each other. must in same cpp file, or must use header file.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -