问题
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);
}
.
But it shows that "Call to 'exp' is ambiguous". Why do I get this message and how will I solve it?
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
回答1:
I suggest this to be closed or deleted by OP. The question simply exhibits some allowed-but-not-recommended C++ usage:
- 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
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>
来源:https://stackoverflow.com/questions/46646986/rcpp-warning-call-to-exp-is-ambiguous