[Rcpp-devel] Issue with Matrices and Iterators

Alessandro Mammana mammana at molgen.mpg.de
Mon Oct 14 21:21:03 CEST 2013

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!

More information about the Rcpp-devel mailing list