Rcpp Compilation Error

I am trying to compile a simple Rcpp example from Rcpp with inline:

Rcpp::NumericVector orig(vector);                  
Rcpp::NumericVector vec(orig.size());          
std::transform(orig.begin(),orig.end(),vec.begin(),sqrt);

return Rcpp::List::create(Rcpp::Named("result")=vec,Rcpp::Named("original") =orig);

However, I get the following error:

no matching function for call to 'transform(Rcpp::traits::storage_type<14>::type*, Rcpp::traits::storage_type<14>::type*, Rcpp::traits::storage_type<14>::type*, <unresolved overloaded function type>)

I use Windows XP with Rtools (other examples without STL working!), With R 2.12.0.

+3
source share
1 answer

Ahhh. sqrt()is now overloaded in Rcpp sugar, so you need to explicitly refer to a character from the C ++ global namespace that is imported from C. Try this line instead:

std::transform(orig.begin(),orig.end(),vec.begin(),::sqrt);

which he works with here:

R> require(inline)
R> src <- '
+     Rcpp::NumericVector orig(vector);
+     Rcpp::NumericVector vec(orig.size());
+     std::transform(orig.begin(), orig.end(), vec.begin(), ::sqrt);
+     return Rcpp::List::create(Rcpp::Named("result") = vec,
+                               Rcpp::Named("original") = orig);
+ '
R> fun <- cxxfunction(signature(vector="numeric"), src, plugin="Rcpp")
R> fun(1:9)
$result
[1] 1.00000 1.41421 1.73205 2.00000 2.23607 2.44949 2.64575 2.82843 3.00000

$original
[1] 1 2 3 4 5 6 7 8 9

R> 

Can you send me the URL of the page / example for which an update is required?

+6
source

All Articles