问题描述
我正在编写一个Rcpp代码,如下所示:
I am writing a Rcpp code as below:
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins(cpp11)]]
#include <RcppArmadillo.h>
#include <boost/random.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include <math.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
double ks(const double k, const double alpha, const double mag, const double M0){
double ksres;
ksres= k* std::exp ( alpha*(mag-M0) );
return(ksres);
}
.
但是它表明调用'exp'是模棱两可的".为什么会收到此消息,我将如何解决?
But it shows that "Call to 'exp' is ambiguous". Why do I get this message and how will I solve it?
当我进入sessionInfo()时:
While I get in sessionInfo():
R version 3.2.4 (2016-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.12.6 (unknown)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Rcpp_0.12.4
loaded via a namespace (and not attached):
[1] colorspace_1.2-6 scales_0.4.0 plyr_1.8.3 tools_3.2.4 inline_0.3.14 gtable_0.2.0 rstan_2.9.0-3
[8] gridExtra_2.2.1 ggplot2_2.1.0 grid_3.2.4 munsell_0.4.3 stats4_3.2.4
推荐答案
我建议OP将其关闭或删除.这个问题只是展示了一些允许但不建议使用的C ++用法:
I suggest this to be closed or deleted by OP. The question simply exhibits some allowed-but-not-recommended C++ usage:
- 包括了额外的标头:数学标头 已由Rcpp引入(由RcppArmadillo引入)
- 您永远都不需要两者
cmath
和math.h
,而且如此处所述,您也不需要 - 我们通常建议不要无条件地平整所有名称空间
- extra headers included: the math headers are already brought in by Rcpp (which is brought in by RcppArmadillo)
- you never ever need both
cmath
andmath.h
, and as stated here you do not need either - we generally recommend against flattening all namespaces unconditionally
这样,您的代码看起来像这样(仍然包含未使用的C ++ 11调用,但没有害处):
With this, your code looks like this (still containing a call for C++11 which is not used, but does no harm):
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins(cpp11)]]
#include <RcppArmadillo.h>
#include <boost/random.hpp>
#include <boost/random/uniform_real_distribution.hpp>
// [[Rcpp::export]]
double ks(const double k, const double alpha, const double mag, const double M0){
double ksres;
ksres= k* std::exp ( alpha*(mag-M0) );
return(ksres);
}
/*** R
ks(1.0, 2.0, 3.0, 4.0)
*/
这在我的盒子上编译时没有任何警告(打开了严格的编译器警告,此处未显示输出)并且也按预期运行:
This compiles without any warning whatsoever on my box (with stringent compiler warnings turned on, output not shown here) and runs as expected too:
R> Rcpp::sourceCpp("/tmp/soQ.cpp")
R> ks(1.0, 2.0, 3.0, 4.0)
[1] 0.135335
R>
这篇关于RCPP警告:对"exp"的调用含糊不清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!