[Rcpp-devel] Picking off a matrix-valued list element inside a class constructor?

Michael Hannon jm_hannon at yahoo.com
Tue Oct 25 01:10:31 CEST 2011


>> the seemingly equivalent statement in the body of a class constructor fails
>> with a compilation error:
>> 
>>   includes = '
>>   class CCC{
>>   public:
>>     CCC(Rcpp::List l){
>>       m = l["m"];
>>     }
>>   [...]
>>   private:
>>     Rcpp::IntegerMatrix m;
>>   }
>>   '

> Here you are trying to use the assignment operator, we could not manage to
> remove the ambiguity. 
> 
> In the first example, you use the ctor, and in this case we managed the
> disambiguation. 
> 
> i would advise using this as the ctor of CCC :
> 
>  CCC(Rcpp::List l) : m( l["m"] ) {}

Thanks, Romain.  Pardon my ignorance, but did you mean that I should use that
declaration literally, or was it merely intended to suggest the approach?  I
don't have much experience with C++.

I tried using it literally and still got a compilation error.  Details are in
the appended.

-- Mike


$ R --vanilla < junk.R

R version 2.13.1 (2011-07-08)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-redhat-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>   library(Rcpp)
>   library(inline)
>   
>   includes = '
+   class CCC{
+   public:
+     CCC(Rcpp::List l) { m( l["m"] ) }
+ 
+     Rcpp::IntegerMatrix mGet(){
+       return m;
+     }
+   private:
+     Rcpp::IntegerMatrix m;
+   };
+   '
>   ccode = '
+       Rcpp::List l(l_R);
+       CCC ccc(l_R);
+       return Rcpp::wrap(ccc.mGet());
+     '
>   fn = cxxfunction(
+     sig      = signature(l_R = "list"),
+     body     = ccode,
+     includes = includes,
+     plugin   = "Rcpp")
file1781a7ea.cpp: In constructor ‘CCC::CCC(Rcpp::List)’:
file1781a7ea.cpp:22:35: error: no match for call to ‘(Rcpp::IntegerMatrix {aka
Rcpp::Matrix<13>}) (Rcpp::Vector<19>::NameProxy)’
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:26:7: note: candidates
are:
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:126:18: note:
Rcpp::Matrix<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&,
const size_t&) [with int RTYPE = 13, Rcpp::Matrix<RTYPE>::Proxy = int&, size_t
= long unsigned int]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:126:18: note:
candidate expects 2 arguments, 1 provided
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:132:18: note:
Rcpp::Matrix<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&,
const size_t&) const [with int RTYPE = 13, Rcpp::Matrix<RTYPE>::Proxy = int&,
size_t = long unsigned int]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:132:18: note:
candidate expects 2 arguments, 1 provided
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:142:16: note:
Rcpp::Matrix<RTYPE>::Row Rcpp::Matrix<RTYPE>::operator()(int,
Rcpp::internal::NamedPlaceHolder) [with int RTYPE = 13,
Rcpp::Matrix<RTYPE>::Row = Rcpp::MatrixRow<13>]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:142:16: note:
candidate expects 2 arguments, 1 provided
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:146:19: note:
Rcpp::Matrix<RTYPE>::Column
Rcpp::Matrix<RTYPE>::operator()(Rcpp::internal::NamedPlaceHolder, int) [with
int RTYPE = 13, Rcpp::Matrix<RTYPE>::Column = Rcpp::MatrixColumn<13>]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:146:19: note:
candidate expects 2 arguments, 1 provided
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:150:16: note:
Rcpp::Matrix<RTYPE>::Sub Rcpp::Matrix<RTYPE>::operator()(const Rcpp::Range&,
const Rcpp::Range&) [with int RTYPE = 13, Rcpp::Matrix<RTYPE>::Sub =
Rcpp::SubMatrix<13>]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:150:16: note:
candidate expects 2 arguments, 1 provided
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:154:16: note:
Rcpp::Matrix<RTYPE>::Sub
Rcpp::Matrix<RTYPE>::operator()(Rcpp::internal::NamedPlaceHolder, const
Rcpp::Range&) [with int RTYPE = 13, Rcpp::Matrix<RTYPE>::Sub =
Rcpp::SubMatrix<13>]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:154:16: note:
candidate expects 2 arguments, 1 provided
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:158:16: note:
Rcpp::Matrix<RTYPE>::Sub Rcpp::Matrix<RTYPE>::operator()(const Rcpp::Range&,
Rcpp::internal::NamedPlaceHolder) [with int RTYPE = 13,
Rcpp::Matrix<RTYPE>::Sub = Rcpp::SubMatrix<13>]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:158:16: note:
candidate expects 2 arguments, 1 provided
file1781a7ea.cpp:22:37: error: expected ‘;’ before ‘}’ token
make: *** [file1781a7ea.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration
errors!

Program source:
  1: 
  2: // includes from the plugin
  3: 
  4: #include <Rcpp.h>
  5: 
  6: 
  7: #ifndef BEGIN_RCPP
  8: #define BEGIN_RCPP
  9: #endif
 10: 
 11: #ifndef END_RCPP
 12: #define END_RCPP
 13: #endif
 14: 
 15: using namespace Rcpp;
 16: 
 17: 
 18: // user includes
 19: 
 20:   class CCC{
 21:   public:
 22:     CCC(Rcpp::List l) { m( l["m"] ) }
 23: 
 24:     Rcpp::IntegerMatrix mGet(){
 25:       return m;
 26:     }
 27:   private:
 28:     Rcpp::IntegerMatrix m;
 29:   };
 30:   
 31: 
 32: // declarations
 33: extern "C" {
 34: SEXP file1781a7ea( SEXP l_R) ;
 35: }
 36: 
 37: // definition
 38: 
 39: SEXP file1781a7ea( SEXP l_R ){
 40: BEGIN_RCPP
 41: 
 42:       Rcpp::List l(l_R);
 43:       CCC ccc(l_R);
 44:       return Rcpp::wrap(ccc.mGet());
 45:     
 46: END_RCPP
 47: }
 48: 
 49: 
Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! file1781a7ea.cpp: In
constructor ‘CCC::CCC(Rcpp::List)’:
file1781a7ea.cpp:22:35: error: no match for call to ‘(Rcpp::IntegerMatrix {aka
Rcpp::Matrix<13>}) (Rcpp::Vector<19>::NameProxy)’
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:26:7: note: candidates
are:
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:126:18: note:
Rcpp::Matrix<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&,
const size_t&) [with int RTYPE = 13, Rcpp::Matrix<RTYPE>::Proxy = int&, size_t
= long unsigned int]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:126:18: note:
candidate expects 2 arguments, 1 provided
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h:132:18: note:
Rcpp::Matrix<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&,
const size_t&) const [with int RTYPE = 13, Rcpp::Matrix<RTYPE>::Proxy = int&,
size_t = long unsigned int]
/usr/lib64/R/library/Rcpp/include/Rcpp/vector/Matrix.h
Calls: cxxfunction -> compileCode
In addition: Warning message:
running command '/usr/lib64/R/bin/R CMD SHLIB file1781a7ea.cpp 2>
file1781a7ea.cpp.err.txt' had status 1 
Execution halted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20111024/bd0afe5d/attachment-0001.htm>


More information about the Rcpp-devel mailing list