Using a C ++ function in another C ++ function (inside an R package with Rcpp)

I am trying to create an R package (using Rcpp). Everything works fine. But now I have written a C ++ function that I want to call in another C ++ file. So, / src has:

  • function1 = linearInterpolation.cpp
  • function2 = getSlope.cpp

Take function 1, which simply performs linear interpolation between two points at a specific position.

#include <Rcpp.h>

using namespace Rcpp;

//' @name linearInterpolation
//' @title linearInterpolation
//' @description Twodimensional linearinterpolation for a specific point
//' @param xCoordinates Two x coordinates
//' @param yCoordinates Coresponding two y coordinates
//' @param atPosition The Point to which the interpolation shall be done
//' @return Returns the linear interpolated y-value for the 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 the slope is calculated in another function (file).

#include <Rcpp.h>

using namespace Rcpp;

//' @name getSlope
//' @title getSlope
//' @description Calculates the slopes between two points in 2Dimensions
//' @param xCoordinates Two x coordinates
//' @param yCoordinates Coresponding two y coordinates
//' @return Returns the 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 don't have any deeper knowledge in Rcpp or C ++. I read Vignette and wrote a package that uses Rcpp. I guess I also read the correct parts, but I didn't get it.

Why is the getSlope function not "visible" in the other function - since they are both in the same package. How can I use getSlope in another file?

Sorry, but I'm really stuck.

Thanks and best wishes

Nico

+3


source to share


1 answer


Perhaps you should make another file, a header file, .hpp

and put it in it:

#include <Rcpp.h>
using namespace Rcpp;
double getSlope(NumericVector xCoordinates, NumericVector yCoordinates);

      

or better yet

#include <Rcpp.h>
double getSlope(Rcpp:: NumericVector xCoordinates, Rcpp:: NumericVector yCoordinates);

      



and place #include"myheader.hpp"

at the top of both cpp files. This is in order to declare this function so that both cpp files can see it.


... since they are both in the same package

Just because two things are in the same R package does not mean that they are in the same C ++ translation system (i.e. cpp file), but the translation unit is important for the two C ++ functions. to see each other. They must be in the same cpp file, or you must use a header file.

+3


source







All Articles