This forum has been archived. All content is frozen. Please use KDE Discuss instead.

This code won't compile

Tags: None
(comma "," separated)
raman
Registered Member
Posts
8
Karma
0
OS

This code won't compile

Thu Jun 28, 2012 4:14 am
Hi, I am trying to write a function that performs a write operation on an input parameter that can either be a vector or a row of a matrix as shown below. Could someone tell me why this code won't compile?
Code: Select all
#include <Eigen/Dense>
using namespace Eigen;

template<typename somevector>
void processVector(somevector& x)
{
   x(0) = 1;
}

int main()
{
  MatrixXf m(3,3);
  m << 1,2,3,
       4,5,6,
       7,8,9;
  processVector(m.row(0));
}


This is the error I get
Code: Select all
$ g++ -Ieigen3 -o eigenX eigenX.cc
eigenX.cc: In function ‘int main()’:
eigenX.cc:16:25: error: invalid initialization of non-const reference of type ‘Eigen::Block<Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>, 1, -0x00000000000000001, false, true>&’ from an rvalue of type ‘Eigen::DenseBase<Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001> >::RowXpr {aka Eigen::Block<Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>, 1, -0x00000000000000001, false, true>}’
eigenX.cc:5:6: error: in passing argument 1 of ‘void processVector(somevector&) [with somevector = Eigen::Block<Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>, 1, -0x00000000000000001, false, true>]’


However, if I only want read access to the vector in the function, I could use a const qualifier and everything compiles fine
Code: Select all
void processVector(const somevector& x)
{
   // do some read-only stuff with x
}


It's probably not eigen, but a C++ thing. Any help would be appreciated.
Thanks!
Raman
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: This code won't compile

Thu Jun 28, 2012 6:49 am
That's indeed a C++ restriction. See this documentation page:

http://eigen.tuxfamily.org/dox/TopicFun ... Types.html

and we are working on a more general solution there:

http://eigen.tuxfamily.org/bz/show_bug.cgi?id=482


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]