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

Declaration and Definition of Eigen vectors

Tags: None
(comma "," separated)
nina1983
Registered Member
Posts
3
Karma
0
Dear,

I'm quite new to Eigen and I have (perhaps) a very trivial question.
Suppose I have a class with a dynamic size vector as private member of the class

class A{
public:
A()
private:
VectorXd v;
}

in the constructor of the class I want to define the size of the vector in this way

A::A(){
v(10);
}

it compiles but I get an error in the execution. Instead if I write

A::A(){
v.resize(10,1);
}

it compiles and execute without errors.
Why it's not possible to do it in the first way? Which is the best way to do it?

Thanks in advance,
Lisa
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
hi,

the correct C++ syntax for your ctor is:

A::A() : v(10) {}
nina1983
Registered Member
Posts
3
Karma
0
mmmh.... thanks for your reply!!!
But I have another question... in case the dimension of the vector is calculed at the beginning of the class constructor (for example it is equal to the number of lines of a file)

A::A(){

// procedure to get the number of line of a given file -> n
....

//initialize the vector to n components
v(n);

}

it's not possible to do in the way you suggest me because I don't know a priori the value of n... how shall I do?

Thanks in advance

Lisa
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
well, when you have:

A::A()
{

at that point the Matrix object v is already constructed: implicitly the default ctor Matrix::Matrix() has been called on v. So you cannot reconstruct it! (well you could recall the ctor using new but that's really not recommended). Here simply call the resize function:

v.resize(10);

just like you would do with a std::vector.
nina1983
Registered Member
Posts
3
Karma
0
Thanks!!!!!!!!!!!!!


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]