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

Michael Hannon jm_hannon at yahoo.com
Sun Oct 23 05:52:25 CEST 2011


Greetings.  I'm trying to pick off, in C++, one component, which is a matrix,
from an R list.

In the first appended example, starting with:

    ** A list containing a matrix

I pass a list from R to C++ and pick it off with a simple assignment
statement:

    ccode = '
        Rcpp::List l(l_R);
        Rcpp::IntegerMatrix m = l["m"];;
        [...]

This seems to work just fine.

But in the second appended example, starting with:

    ** Putting it into a class

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;
  }
  '

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! file33334060.cpp: In
constructor ‘CCC::CCC(Rcpp::List)’:
file33334060.cpp:23:16: error: ambiguous overload for ‘operator=’ in
‘((CCC*)this)->CCC::m = Rcpp::Vector<RTYPE>::operator[](const string&) [with
int RTYPE = 19, Rcpp::Vector<RTYPE>::NameProxy =
Rcpp::internal::generic_name_proxy<19>, std::string =
std::basic_string<char>]((* & std::basic_string<char>(((const char*)"m"),
(*(const std::allocator<char>*)(& std::allocator<char>())))))’
[...]


My sessionInfo() output is at the very bottom of this message.

Any idea what I'm doing wrong?

Thanks,

-- Mike


** A list containing a matrix
#+begin_src R
  library(Rcpp)
  library(inline)
  
  includes = '
  '
  ccode = '
      Rcpp::List l(l_R);
      Rcpp::IntegerMatrix m = l["m"];
      return Rcpp::wrap(m(2,0));
    '
  fn = cxxfunction(
    sig      = signature(l_R = "list"),
    body     = ccode,
    includes = includes,
    plugin   = "Rcpp")
  
  m <- matrix(1:6, ncol = 2)
  l <- list(m = m, a = 14)
  fn(l)
  
#+end_src

#+results:
: 3

** Putting it into a class
#+begin_src 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")
  
  m <- matrix(1:6, ncol = 2)
  l <- list(m = m, a = 14)
  fn(l)
  
#+end_src

#+results:



> sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=C
LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base  



More information about the Rcpp-devel mailing list