No subject


Mon Oct 17 11:22:36 CEST 2011


class World {
public:
    World() : msg("hello"){}
    void set(std::string msg) { this->msg =3D msg; }
    std::string greet() { return msg; }
    void addMsg(World& w2) {
msg =3D msg + w2.msg;
    }
private:
    std::string msg;
};

However, this gives errors of the sort
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_Cpp=
Method.h:186:4:
error: invalid initialization of non-const reference of type =91World&=92 f=
rom
an rvalue of type =91World=92
In file included from
/usr/local/lib/R/site-library/Rcpp/include/RcppCommon.h:306:0,
                 from /usr/local/lib/R/site-library/Rcpp/include/Rcpp.h:27,
                 from rcpp_module.cpp:1:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/traits/Exporter.h: In
constructor =91Rcpp::traits::Exporter<T>::Exporter(SEXPREC*) [with T =3D Wo=
rld,
SEXPREC* =3D SEXPREC*]=92:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:51:51:   instantiated
from =91T Rcpp::internal::as(SEXPREC*, Rcpp::traits::r_type_generic_tag)
[with T =3D World, SEXPREC* =3D SEXPREC*]=92
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:75:89:   instantiated
from =91T Rcpp::as(SEXPREC*) [with T =3D World, SEXPREC* =3D SEXPREC*]=92
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_Cpp=
Method.h:186:4:
  instantiated from =91SEXPREC* Rcpp::CppMethod1<Class, void,
U0>::operator()(Class*, SEXPREC**) [with Class =3D World, U0 =3D World&,
SEXPREC* =3D SEXPREC*]=92
rcpp_module.cpp:86:1:   instantiated from here
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/traits/Exporter.h:31:37:
error: no matching function for call to =91World::World(SEXPREC*&)=92
rcpp_module.cpp:30:5: note: candidates are: World::World()
rcpp_module.cpp:28:13: note:                 World::World(const World&)
make: *** [rcpp_module.o] Error 1

Looking at http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pdf, it
seems that I can extend Rcpp::as to account for my classes, but I still
have two questions:
1. How do I define the constructor World(SEXP)? Is there a working example
somewhere for a user defined C++ class?
2. Can I also use pointers and pass-by-reference as these are more often
used in real C++ code.

In short, I'd appreciate a way to pass my C++ classes (as the class and
also by ref. or pointer) that have been exposed to R as arguments to
exposed functions. How do I achieve that?

Thanks!

--001636c926e30fec3704b4973f0a
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

I need to pass a class as an argument in a function I am exposing to R. Fro=
m the skeleton package module, let us say that I have<div><br></div><div><d=
iv>class World {</div><div>public:</div><div>=A0 =A0 World() : msg(&quot;he=
llo&quot;){}</div>
<div>=A0 =A0 void set(std::string msg) { this-&gt;msg =3D msg; }</div><div>=
=A0 =A0 std::string greet() { return msg; }</div><div>=A0 =A0 void addMsg(W=
orld&amp; w2) {</div><div><span class=3D"Apple-tab-span" style=3D"white-spa=
ce:pre">	</span>msg =3D msg + w2.msg;</div>
<div>=A0 =A0 }</div><div>private:</div><div>=A0 =A0 std::string msg;</div><=
div>};</div><div><br></div></div><div>However, this gives errors of the sor=
t</div><div><div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Mod=
ule_generated_CppMethod.h:186:4: error: invalid initialization of non-const=
 reference of type =91World&amp;=92 from an rvalue of type =91World=92</div=
>
<div>In file included from /usr/local/lib/R/site-library/Rcpp/include/RcppC=
ommon.h:306:0,</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0from /usr/local=
/lib/R/site-library/Rcpp/include/Rcpp.h:27,</div><div>=A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0from rcpp_module.cpp:1:</div>
<div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/traits/Exporter.h: In =
constructor =91Rcpp::traits::Exporter&lt;T&gt;::Exporter(SEXPREC*) [with T =
=3D World, SEXPREC* =3D SEXPREC*]=92:</div><div>/usr/local/lib/R/site-libra=
ry/Rcpp/include/Rcpp/as.h:51:51: =A0 instantiated from =91T Rcpp::internal:=
:as(SEXPREC*, Rcpp::traits::r_type_generic_tag) [with T =3D World, SEXPREC*=
 =3D SEXPREC*]=92</div>
<div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:75:89: =A0 instan=
tiated from =91T Rcpp::as(SEXPREC*) [with T =3D World, SEXPREC* =3D SEXPREC=
*]=92</div><div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Modu=
le_generated_CppMethod.h:186:4: =A0 instantiated from =91SEXPREC* Rcpp::Cpp=
Method1&lt;Class, void, U0&gt;::operator()(Class*, SEXPREC**) [with Class =
=3D World, U0 =3D World&amp;, SEXPREC* =3D SEXPREC*]=92</div>
<div>rcpp_module.cpp:86:1: =A0 instantiated from here</div><div>/usr/local/=
lib/R/site-library/Rcpp/include/Rcpp/traits/Exporter.h:31:37: error: no mat=
ching function for call to =91World::World(SEXPREC*&amp;)=92</div><div>rcpp=
_module.cpp:30:5: note: candidates are: World::World()</div>
<div>rcpp_module.cpp:28:13: note: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 World::Wo=
rld(const World&amp;)</div><div>make: *** [rcpp_module.o] Error 1</div></di=
v><div><br></div><div>Looking at=A0<a href=3D"http://dirk.eddelbuettel.com/=
code/rcpp/Rcpp-extending.pdf">http://dirk.eddelbuettel.com/code/rcpp/Rcpp-e=
xtending.pdf</a>, it seems that I can extend Rcpp::as to account for my cla=
sses, but I still have two questions:</div>
<div>1. How do I define the constructor World(SEXP)? Is there a working exa=
mple somewhere for a user defined C++ class?</div><div>2. Can I also use po=
inters and pass-by-reference as these are more often used in real C++ code.=
</div>
<div><br></div><div>In short, I&#39;d appreciate a way to pass my C++ class=
es (as the class and also by ref. or pointer) that have been exposed to R a=
s arguments to exposed functions. How do I achieve that?</div><div><br>
</div><div>Thanks!</div>

--001636c926e30fec3704b4973f0a--


More information about the Rcpp-devel mailing list