简体   繁体   English

在其他c ++函数中使用c ++函数(在Rcpp中使用Rcpp)

[英]Using a c++ function in an other c++ function (inside a R Package with Rcpp)

I try to create a R package (using Rcpp). 我尝试创建一个R包(使用Rcpp)。 Everything works fine. 一切正常。 But now I wrote a c++ function, which I want to call in an other c++ file. 但现在我写了一个c ++函数,我想在其他c ++文件中调用它。 So in /src there is: 所以在/ src中有:

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

Let's take function1, which just do a linear interpolation between two points at a specific position. 让我们看一下function1,它只是在特定位置的两个点之间进行线性插值。

#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 a different 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 do not have any deeper knowledge in Rcpp or c++. 我对Rcpp或c ++没有更深入的了解。 I read the Vignette and Writing a package that uses Rcpp I think I also read the right parts but I didn't get it. 我阅读了Vignette并编写了一个使用Rcpp的软件包我想我也读过正确的部分,但我没有得到它。

Why is the getSlope function not "visible" in the other function - as they are both in the same package. 为什么getSlope函数在另一个函数中不“可见” - 因为它们都在同一个包中。 How can I use the getSlope in the other file? 如何在其他文件中使用getSlope?

Sorry but I am really stuck. 对不起,我真的被困了。

Thanks and best Regards 谢谢,最诚挚的问候

Nico 尼科

Perhaps you should make another file, a header file .hpp , and put this in it: 也许你应该制作另一个文件,一个头文件.hpp ,并将其放入其中:

#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" at the top of both of your cpp files. 并将#include"myheader.hpp"放在两个cpp文件的顶部。 This is in order to declare this function such that both cpp files can see it. 这是为了声明这个函数,这样两个cpp文件都可以看到它。


... as they are both in the same package ......因为他们都在同一个包里

Just because two things are in the same R package, that does not mean they are in the same C++ translation unit (ie cpp file), and the translation unit is the important thing for two C++ functions to see each other. 仅仅因为两个东西在同一个R包中,这并不意味着它们在同一个C ++ 转换单元 (即cpp文件)中,并且转换单元对于两个C ++函数来看彼此是重要的。 They must be in the same cpp file, or you must use a header file. 它们必须位于同一个cpp文件中,或者您必须使用头文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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