<div dir="ltr"><div>Hello Finlay,<br><br>I believe that I ran into a similar problem a few weeks ago.  Maybe you can find it on the recent archive of the list.  I was having a similar issue with RcppExports.cpp not compiling because it couldn't recognize a type.  <br>
<br>What ended up working for me was to use the RcppAttributes 'interface' functionality.  It's described in section 3.5 of the RcppAttributes vignette.  By adding '// [[Rcpp::interfaces(r, cpp)]]' to one of your .cpp source files Rcpp will automatically build the header (named after the package) that JJ was alluding to and then you can manually modify it to include the other headers that you need.  By deleting the auto-generated key at the top of the 'asWrapExample.h' header (in your case) Rcpp won't re-generate it and it is automatically included in RcppExports.cpp, along with your specialized headers required for your package.<br>
<br></div><div>I don't know why just manually creating it yourself like JJ suggested and what you seem to do didn't work, but maybe going through the package's auto-generation stuff will.<br></div><div><br></div>
Hope that helps,<br><br>Alex <br><div><div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote"><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<br>
Thanks for the reply. I followed your suggestion and changed the name of<br>
the header file in /inst/include to 'asWrapExample.h' (the name of the<br>
package). I then changed the #include in the 'DummyClass_example.cpp' file<br>
to reflect that change. I also added:<br>
<br>
PKG_CXXFLAGS=-I../inst/include<br>
<br>
To the top of the Makevars file. However, I still get the same error. I<br>
have the feeling I'm missing something obvious. Thanks for the help so far.<br>
<br>
Yours<br>
<br>
Finlay<br>
<br>
<br>
<br>
On Wed, Apr 24, 2013 at 5:41 PM, JJ Allaire <<a href="mailto:jj.allaire@gmail.com" target="_blank">jj.allaire@gmail.com</a>> wrote:<br>
<br>
> Hi Finlay,<br>
><br>
> If you name your include file the same name as your package then it will<br>
> be included automatically in RcppExports.cpp. The convention at work here<br>
> is that any custom as/wrap handlers should be accumulated (or referenced<br>
> from) that single file. This mirrors the existing convention used by Rcpp,<br>
> RcppArmadillo, RcppEigen, etc. to have a single global header file for<br>
> their C++ API.<br>
><br>
> J.J.<br>
><br>
><br>
> On Wed, Apr 24, 2013 at 8:22 AM, Finlay Scott <<a href="mailto:drfinlayscott@gmail.com" target="_blank">drfinlayscott@gmail.com</a>>wrote:<br>
><br>
>> Hi<br>
>> First of all I want to say how impressed I am with Rcpp. I think it is<br>
>> going to be very useful for some of the packages I am developing. Thank you<br>
>> very much for developing it.<br>
>><br>
>> I have a question regarding writing custom as and wrap functions for my<br>
>> own classes. Following the example in:<br>
>><br>
>> <a href="http://gallery.rcpp.org/articles/custom-as-and-wrap-example/" target="_blank">http://gallery.rcpp.org/articles/custom-as-and-wrap-example/</a><br>
>><br>
>> I can get my own minimal example to work with a very simple class, and<br>
>> using the sourceCpp() function.<br>
>><br>
>> The cpp code saved as a *.cpp file:<br>
>><br>
>>     #include <RcppCommon.h><br>
>><br>
>>     class DummyClass {<br>
>>         public:<br>
>>             double value;<br>
>>     };<br>
>><br>
>>     namespace Rcpp {<br>
>>         // non-intrusive extension via template specialisation<br>
>>         template <> DummyClass as(SEXP dt);<br>
>>         // non-intrusive extension via template specialisation<br>
>>         template <> SEXP wrap(const DummyClass &d);<br>
>>     }<br>
>><br>
>>     #include <Rcpp.h><br>
>><br>
>>     // define template specialisations for as and wrap<br>
>>     namespace Rcpp {<br>
>>         template <> DummyClass as(SEXP dtsexp) {<br>
>>         S4 dc_s4 = Rcpp::as<S4>(dtsexp);<br>
>>         DummyClass dc;<br>
>>         dc.value = dc_s4.slot("value");<br>
>>         return dc;<br>
>>         }<br>
>><br>
>>         template <> SEXP wrap(const DummyClass &d) {<br>
>>         Rcpp::S4 dc_s4("DummyClass");<br>
>>         dc_s4.slot("value") = d.value;<br>
>>         return Rcpp::wrap(dc_s4);<br>
>>         }<br>
>>     }<br>
>><br>
>>     // [[Rcpp::export]]<br>
>>     DummyClass test_as_wrap(DummyClass dc, double multiplier){<br>
>>         DummyClass dc_out;<br>
>>         dc_out.value = dc.value * multiplier;<br>
>>         return dc_out;<br>
>>     }<br>
>><br>
>><br>
>> And the following R code compiles and calls the function:<br>
>><br>
>>     library(Rcpp)<br>
>>     sourceCpp("DummyClass_example.cpp")<br>
>>     setClass("DummyClass", representation(value = "numeric"))<br>
>>     dc <- new("DummyClass")<br>
>>     dc@value <- 23<br>
>>     test_as_wrap(dc, 4)<br>
>><br>
>> This works just fine (like magic!) and the test_as_wrap() function is<br>
>> happily called from R and returns an object of type DummyClass. I want to<br>
>> use a similar approach in a package, so I made a minimal package using:<br>
>><br>
>> Rcpp.package.skeleton("asWrapExample",attributes=TRUE)<br>
>><br>
>> I then split my original cpp file above into header and source code<br>
>> files. In the /inst/include directory I placed a file<br>
>> 'DummyClass_example.h' which has:<br>
>><br>
>>     #include <RcppCommon.h><br>
>><br>
>>     class DummyClass {<br>
>>         public:<br>
>>             double value;<br>
>>     };<br>
>><br>
>>     namespace Rcpp {<br>
>>         // non-intrusive extension via template specialisation<br>
>>         template <> DummyClass as(SEXP dt);<br>
>>         // non-intrusive extension via template specialisation<br>
>>         template <> SEXP wrap(const DummyClass &d);<br>
>>     }<br>
>><br>
>> In the /src directory I placed a file 'DummyClass_example.cpp' which has:<br>
>><br>
>>     #include "../inst/include/DummyClass_example.h"<br>
>>     #include <Rcpp.h><br>
>><br>
>>     // define template specialisations for as and wrap<br>
>>     namespace Rcpp {<br>
>>         template <> DummyClass as(SEXP dtsexp) {<br>
>>         S4 dc_s4 = Rcpp::as<S4>(dtsexp);<br>
>>         DummyClass dc;<br>
>>         dc.value = dc_s4.slot("value");<br>
>>         return dc;<br>
>>         }<br>
>><br>
>>         template <> SEXP wrap(const DummyClass &d) {<br>
>>         Rcpp::S4 dc_s4("DummyClass");<br>
>>         dc_s4.slot("value") = d.value;<br>
>>         return Rcpp::wrap(dc_s4);<br>
>>         }<br>
>>     }<br>
>><br>
>>     // [[Rcpp::export]]<br>
>>     DummyClass test_as_wrap(DummyClass dc, double multiplier){<br>
>>         DummyClass dc_out;<br>
>>         dc_out.value = dc.value * multiplier;<br>
>>         return dc_out;<br>
>>     }<br>
>><br>
>> When I try to compile the package I get this error message:<br>
>><br>
>>     RcppExports.cpp:9:1: error: 'DummyClass' does not name a type<br>
>><br>
>> This is probably caused by the RcppExports.cpp not having an #include for<br>
>> my DummyClass_example.h.<br>
>> I understand the RcppExports.cpp file is automatically generated by the<br>
>> magic of Rcpp so there is no point in adding it there by hand.<br>
>> I've looked at the documentation but it is not clear to me how I can tell<br>
>> RcppExports to also include my header file (if this is the source of the<br>
>> problem).<br>
>> Have I missed something in the documentation, or is there an example I<br>
>> can follow?<br>
>> Any help is appreciated.<br>
>><br>
>> Yours<br>
>><br>
>> Finlay<br>
<br>
</blockquote></div><br></div></div></div></div></div></div>