我看过其他有关此问题的文章。我跟着他们。
我用于将双精度数组转换为双精度向量的代码是-
cppFunction("
std::vector<double> ArrayToVector(double *arr){
std::vector<double> vec(arr, arr+ sizeof(arr)/sizeof(arr[0]));
return vec;
}
")
但是我收到此错误:
file52e20f14d34.cpp:28:52: error: cannot initialize a variable of type 'Rcpp::traits::input_parameter<double>::type *' (aka 'InputParameter<double> *') with an lvalue of type 'SEXP' (aka 'SEXPREC *')
Rcpp::traits::input_parameter< double >::type *arr(*arrSEXP);
^ ~~~~~~~~
1 error generated.
make: *** [file52e20f14d34.o] Error 1
clang++ -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I"/private/var/folders/4w/v4pl36r9475cb2fkspc3qxkm0000gp/T/RtmpSXqByM/sourceCpp-x86_64-apple-darwin15.6.0-1.0.1" -I/usr/local/include -fPIC -Wall -g -O2 -c file52e20f14d34.cpp -o file52e20f14d34.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, :
Error 1 occurred building shared library.
有人可以解释我为什么会这样吗?任何帮助将不胜感激。
答案 0 :(得分:2)
您不能从double*
推断出数组的长度(除非有一个特殊的double
值用作数组终止符)。数组大小也必须传递给函数:
std::vector<double> ArrayToVector(double* arr, size_t arr_len) {
return std::vector<double>(arr, arr + arr_len);
}