Hi all,

I'm having a simple problem with modules - when I instantiating the
object and call the methods in C++, I get a different answer from
instantiating and calling the methods in R via a module.  I've
attached a simple test case that you can run with sourceCpp() - when I
run it (using svn rev 4264 or CRAN version) I get 0 instead of 8. What
am I doing wrong?

Thanks!

Hadley

-- 
Chief Scientist, RStudio
http://had.co.nz/
// Sys.setenv("CXXFLAGS" = "-g -O0")

#include <Rcpp.h>
using namespace Rcpp;

class GroupFixed {
    const NumericVector& x_;
    double width_;
    double origin_;
  public:
    GroupFixed (const NumericVector& x, double width, double origin = 0)
       : x_(x), width_(width), origin_(origin) {
    }

    int bin(int i) const {
      if (x_[i] < origin_) return 0;
      return (x_[i] - origin_) / width_;
    }
};

RCPP_MODULE(Group) {
  class_<GroupFixed>( "GroupFixed")
  .constructor<const NumericVector&, double, double>()
  .const_method("bin", &GroupFixed::bin)
  ;
}


// [[Rcpp::export]]
int test(const NumericVector& x) {
  GroupFixed g(x, 0.1, 0);
  return g.bin(1);
}

/*** R

# With C++
x <- runif(100)
test(x)

# With R, using module
last <- function(x) x[[length(x)]]
m <- Module("Group", last(getLoadedDLLs()))
GroupFixed <- m$GroupFixed

g <- GroupFixed$new(x, 0.1, 0)
g$bin(1L)

**/
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to