teucer
teucer

Reputation: 6238

Rcpp compilation error

I am trying to compile the simple Rcpp example from Rcpp webpage 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 am using Windows XP with Rtools (other examples without STL works!), with R 2.12.0.

Upvotes: 4

Views: 1665

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368489

Ahh. sqrt() is now overloaded in Rcpp sugar so you need to explicitly refer to the symbol from the global C++ namespace that is imported from C. So try this line instead:

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

with which it works 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 that needs an update?

Upvotes: 6

Related Questions