【发布时间】:2015-07-14 12:41:48
【问题描述】:
我尝试创建一个 R 包(使用 Rcpp)。 一切正常。但是现在我写了一个 c++ 函数,我想在另一个 c++ 文件中调用它。 所以在 /src 中有:
- function1 = linearInterpolation.cpp
- function2 = getSlope.cpp
让我们以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]);
}
并且斜率在不同的函数(文件)中计算。
#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]);
}
我对 Rcpp 或 c++ 没有任何深入的了解。我阅读了 Vignette 和 Writing a package that uses Rcpp 我想我也阅读了正确的部分,但我没有明白。
为什么 getSlope 函数在另一个函数中不“可见” - 因为它们都在同一个包中。 如何在其他文件中使用 getSlope?
对不起,我真的卡住了。
感谢和最好的问候
尼哥
【问题讨论】:
-
你必须link这两个文件。将它们放在同一个文件中会更容易。
-
请告诉我们您收到的错误信息。简单地说“功能不可见”是不够的。 “可见”是什么意思?
-
嗨 Verena,我不想将它们放入同一个文件中。我最终会陷入多种功能的混乱局面。但谢谢,我会通过你的链接阅读。嗨亚伦,当然,对不起。我得到的错误是:'getSlope' 没有在这个范围内声明