[Rcpp-devel] Issue with Matrices and Iterators

Matteo Fasiolo matteo.fasiolo at gmail.com
Mon Oct 14 21:39:15 CEST 2013

Hi Ale,

 maybe I'm wrong but mat(0,_) is not a NumericVector, but a Rcpp::MatrixRow.
So your function sum() is being called with arguments of the wront type
(the error
message is saying that).

If instead you select a column mat( _, 0) you have a conversion to a
I've encountered the same issue a couple of time.


On Mon, Oct 14, 2013 at 8:21 PM, Alessandro Mammana
<mammana at molgen.mpg.de>wrote:

> Hi all,
> I have very little experience with Rcpp and also with c++, so I am
> sorry if my question might be too stupid.
> I am trying to use iterators in my code, because I understand them
> better than vectors, because they should be efficient, and because I
> should be able to combine different implementations of vector and
> matrix classes. However the following code does not compile:
> #include <Rcpp.h>
> using namespace Rcpp;
> typedef NumericVector::iterator DoubleIter;
> double sum(DoubleIter b, DoubleIter e){
>     double sum = 0;
>     while (b!=e){
>         sum += *(b++);
>     }
>     return sum;
> }
> // [[Rcpp::export]]
> double sumFirstRow(NumericMatrix mat){
>     return sum(mat(0,_).begin(), mat(0,_).end());
> }
> The error message is not very useful (or at least I cannot interpret it):
> In function 'double sumFirstRow(Rcpp::NumericMatrix)':
> error: no matching function for call to
> 'sum(Rcpp::MatrixRow<14>::iterator, Rcpp::MatrixRow<14>::iterator)'
> note: candidates are:
> note: double sum(DoubleIter, DoubleIter)
> note: no known conversion for argument 1 from
> 'Rcpp::MatrixRow<14>::iterator' to 'DoubleIter {aka double*}'
> Am I missing something very basic?
> Thanks a lot!
> Ale
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131014/5fe13c63/attachment.html>

More information about the Rcpp-devel mailing list