好的,所以我正在用 R 编程,我想创建一个 C++ 函数。我已经导入了 Rcpp 和内联库。现在,我只是想创建一个简单的函数来添加 2 个数字,但无论我尝试什么,都会出错。
这是我的代码:
cppstring = 'double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();'
hi <- cfunction(body=cppstring, signature(s="double"), Rcpp = TRUE)
当我进入第二行时,我得到
file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1
ERROR(s) during compilation: source code errors or compiler configuration errors!
Program source:
1: #include <Rcpp.h>
2:
3:
4: extern "C" {
5: SEXP file628a34ce ( SEXP s );
6: }
7:
8: SEXP file628a34ce ( SEXP s ) {
9: double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();
10: Rf_warning("your C program does not return anything!");
11: return R_NilValue;
12: }
Error in compileCode(f, code, language, verbose) :
Compilation ERROR, function(s)/method(s) not created! file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1
我已经尝试了所有我能想到的东西,从强制转换到移动代码,到 #include RcppSexp,再到只是简单地返回 s,每次我遇到一些错误时,无论是
cannot convert ‘double’ to ‘SEXPREC*’ in return
或者
invalid use of undefined type ‘struct SEXPREC’
或者
forward declaration of ‘struct SEXPREC’
...我很困惑:(我在网上查看了几个示例,而我目前所拥有的似乎是其他人正在做的事情,并且它对他们神奇地起作用...
我随处可见的 SEXPREC* 是什么?它产生的外部“C”函数是什么?为什么它会在我的 return 语句之后生成语句并告诉我我的函数没有返回任何东西,即使它确实返回了?