2
votes

when using Rcpp, I want to use function abs, I just write Rcpp::abs(-1), but always an error:

no matching function for call to abs

Actually when I write Rcpp:ab, there are some hint that there exists Rcpp::abs(). I have tried some other function Rcpp::NumericVector, it works. I know I can use std::abs(-1), I just wonder why Rcpp::abs(-1) do not work, my system is windows, and I install Rtools.

1

1 Answers

2
votes

Rcpp::abs() requires an Rcpp object, e.g. *Vector and *Matrix.

Unfortunately, -1 is of a primitive type, e.g. not an Rcpp object.

So, the following would work:

#include <Rcpp.h>
// [[Rcpp::export]]
void show_rcpp_abs() {
    Rcpp::NumericVector A = NumericVector::create(-1);
    Rcpp::Rcout << "A" << Rcpp::abs(A) << std::endl;

    double B = std::abs(-1.0);
    Rcpp::Rcout << "B" << B << std::endl;
}