Hi I am using RCPP to build a C++ function for quicksort called qsort. This function is compiled and loaded through the cxxfunction in R I am getting the message in R error: no matching function for call to 'qsort(int*&)' The code is below. It will not run for me and I was wondering if you could help? library(Rcpp) library(inline) incl <- 'int qsort(int xx[], int left, int right) { int i = left, j = right; int tmp; int pivot = xx[(left + right) / 2]; /* partition */ while (i <= j) { while (xx[i] < pivot) i++; while (xx[j] > pivot) j--; if (i <= j) { tmp = xx[i]; xx[i] = xx[j]; xx[j] = tmp; i++; j--; } } /* recursion */ if (left < j){ qsort(xx, left, j); } if (i < right){ qsort(xx, i, right); } return (qsort(xx)); } ' sortCpp <- cxxfunction(signature( x = "integer",left = "integer", right = "integer"), body = 'IntegerVector arr(x); int a = as<int>(left); int b = as<int>(right); return wrap(qsort(arr));', include = incl, plugin = "Rcpp", verbose = TRUE) [[alternative HTML version deleted]]
Actually sort() is already there .... Best, Uwe Ligges On 24.07.2015 17:22, Martin Tully wrote:> Hi I am using RCPP to build a C++ function for quicksort called qsort. > This function is compiled and loaded through the cxxfunction in R > I am getting the message in R error: no matching function for call to > 'qsort(int*&)' The code is below. > It will not run for me and I was wondering if you could help? > > > > library(Rcpp) > library(inline) > > > incl <- 'int qsort(int xx[], int left, int right) { > > int i = left, > j = right; > int tmp; > int pivot = xx[(left + right) / 2]; > > /* partition */ > while (i <= j) { > while (xx[i] < pivot) > i++; > while (xx[j] > pivot) > j--; > if (i <= j) { > tmp = xx[i]; > xx[i] = xx[j]; > xx[j] = tmp; > i++; > j--; > } > } > > /* recursion */ > if (left < j){ > qsort(xx, left, j); > } > if (i < right){ > qsort(xx, i, right); > } > > return (qsort(xx)); > } > ' > > sortCpp <- cxxfunction(signature( x = "integer",left = "integer", > right = "integer"), > body = 'IntegerVector arr(x); > int a = as<int>(left); > int b = as<int>(right); > return wrap(qsort(arr));', > include = incl, > plugin = "Rcpp", > verbose = TRUE) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
> On 24 Jul 2015, at 17:22 , Martin Tully <tulls4472 at gmail.com> wrote: > > Hi I am using RCPP to build a C++ function for quicksort called qsort. > This function is compiled and loaded through the cxxfunction in R > I am getting the message in R error: no matching function for call to > 'qsort(int*&)' The code is below. > It will not run for me and I was wondering if you could help?I'm too old to be good at C++, but this looks wrong:> > > > library(Rcpp) > library(inline) > > > incl <- 'int qsort(int xx[], int left, int right) { > > ...... > return (qsort(xx)); > } > 'It looks wrong on two counts: qsort() calls itself with no conditioning, and even if that is intentional, the call doesn't match the definition. Shouldn't it just be return(xx); ? -pd -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com