Skip to content
Prev 5940 / 10988 Next

[Rcpp-devel] Simplest Class Example For Starters

hi dirk---it is indeed excellent.  alas, it doesn't cover my confusion.

*general*: I now realize my first problem with the print function.  I need
to double-escape '\'.
   cppFunction('int hello(int i) { printf("hello, world\n"); }')
is wrong.  it needs to be
   cppFunction('int hello(int i) { printf("hello, world\\n"); }')
gotcha!

*
S2.9*: the cppFunction works without the attributes in this examples.  what
determines whether one needs to declare attributes vs. Rcpp being smart
enough to figure it out itself?  in any case, this works:

library( Rcpp )

cppFunction('
struct silly { int a; double *b; };
// [[Rcpp::export]]
long initme(int i) {
  silly *s= new silly;
  s->a=i; s->b=new double[i+1];
  return (long) s;
}
')

cat("Compiled OK\n")
x <- initme(5)


although this works, I still get a warning.

   No function found for Rcpp::export attribute at file284a5ab1f09.cpp:5

but R has picked up the initme() function.  it's a good start.

I presume long is the wrong return type.  (void*) did not work.  what is
the recommended pointer type?

And somehow I need to figure out how to tell R to call a destructor when
the x object goes out of scope.


I don't want to take your time any longer, so I think it is better for me
to give up here.

regards,

/iaw
----
Ivo Welch (ivo.welch at gmail.com)
On Thu, May 30, 2013 at 3:39 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130530/54b59efd/attachment.html>