Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with comma initialisation in Eigen c++

I have a problem where the comma initialisation indicated in the Eigen tutorial here doesn't seem to be working.

I have a system where I have a main section where a vector is initialised:

Main:

VectorXd v;

and a function:

double useVector(VectorXd &v) {
    dataI = model_.find();
    v << model_[dataI].v[0], model_[dataI].v[1], model_[dataI].v[2], 1;
    return dataI;
}

Note: the function is used like this:

double distance = useVector(v);

Now the model_[dataI].v is a double[3] and it is definitely working. My understanding is that this is the same as this:

VectorXd v;
v << 1, 2, 3,
     4, 5, 6,
     7, 8, 9;

but it is not working, the code is seg-faulting at the comma initialization phase in function.

Note that this works:

v.resize(4)
v[0] = model_[dataI].v[0];
v[1] = model_[dataI].v[1];
v[2] = model_[dataI].v[2];
v[3] = 1;

as long as v is initialised, like this:

VectorXd v(4);

which immediately makes me wonder about the point of the resize (but if I take it away then it seg-faults again).

Does anyone know why this is happening?

like image 330
Fantastic Mr Fox Avatar asked Dec 06 '25 16:12

Fantastic Mr Fox


1 Answers

Yes, the vector v must be resized to the appropriate size before using the comma initializer.

like image 148
ggael Avatar answered Dec 08 '25 06:12

ggael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!