Thanks Dirk. Passing the addition function in the includes argument worked great. <div><br></div><div>Also, one of the links from your suggested Google search led me to this (working) example for std::accumulate:</div><div>
<div><br></div><div>src <- 'NumericVector xx(x);</div><div>return wrap( std::accumulate( xx.begin(), xx.end(), 0.0));'</div><div>fx <- cxxfunction(signature( x = "numeric" ),body=src,plugin = "Rcpp")</div>
<div>fx(1:10) # 55</div><div><br></div><div>I had been copying the quickref and using: std::accumulate( xx.begin(), xx.end(),std::plus<double>(),0.0)</div><div><br></div><div>I think this might be a typo where the last two arguments are transposed (which I shouldn't have noticed after browsing the <a href="http://www.cppreference.com/wiki/algorithm/accumulate">function's c++ reference</a>), as the following works just fine: std::accumulate( xx.begin(), xx.end(),0.0,std::plus<double>())</div>
<div><br></div><div>Chris</div><br><div class="gmail_quote">On Sat, May 14, 2011 at 5:29 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
On 14 May 2011 at 17:13, Chris DuBois wrote:<br>
| Thanks Dirk. Quick newbie followup question: Can we put functions within the<br>
| inline C++ code? The following doesn't compile, for example:<br>
| src <- '<br>
| int addition (int a, int b)<br>
| {<br>
| int r;<br>
| r=a+b;<br>
| return (r);<br>
| }<br>
| NumericVector xx(x);<br>
| return(xx);<br>
| '<br>
| testfun = cxxfunction(signature(x="numeric"),body=src,plugin="Rcpp")<br>
<br>
</div>Use the 'verbose=TRUE' to cxxfunction() and it will become clear<br>
why -- a function is written around the 'body=...' argument,<br>
hence no chance that that part may contain another C++ function.<br>
<br>
The remedy is simple: use the "include=..." argument as in<br>
<br>
inc <- '<br>
<div class="im"> int addition (int a, int b) {<br>
</div> int r = a + b;<br>
return (r);<br>
}<br>
'<br>
<br>
src = '<br>
<div class="im"> NumericVector xx(x);<br>
return(xx);<br>
'<br>
<br>
</div> testfun = cxxfunction(signature(x="numeric"), body=src, include=inc, plugin="Rcpp")<br>
<br>
<br>
That trick is used a couple of time in the documentation and examples.<br>
<div class="im"><br>
<br>
| On a related note, would you mind showing a quick working example using<br>
| std::accumulate? The one below from the quick reference doesn't compile for<br>
| me.<br>
<br>
<br>
</div>Try Google for something like that, eg a query that is restricted to my site as in<br>
<br>
site:<a href="http://dirk.eddelbuettel.com" target="_blank">dirk.eddelbuettel.com</a> "std::accumulate"<br>
<br>
show five immediate hits. You can similarly constrict the search<br>
to the rcpp-devel list.<br>
<br>
Can you send a small example for inline that shows how/where it quickref<br>
example fails for you?<br>
<div><div></div><div class="h5"><br>
Dirk<br>
<br>
--<br>
Gauss once played himself in a zero-sum game and won $50.<br>
-- #11 at <a href="http://www.gaussfacts.com" target="_blank">http://www.gaussfacts.com</a><br>
</div></div></blockquote></div><br></div>