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

Assertion `index >= 0 && index < size()' failed. C++

Tags: None
(comma "," separated)
riotburn
Registered Member
Posts
1
Karma
0
I keep getting that assertion failure when I run my code. Can anyone tell me whats wrong here? The code is a simple forward substitution function:

Code: Select all
Eigen::VectorXd forward_subst(Eigen::MatrixXd L, Eigen::VectorXd b)
{
   Eigen::VectorXd x;

   x(0) = b(0)/L(0, 0);

   for(int k = 1; k < L.cols(); k++)
   {
      double sum = 0;
      for(int i = 0; i < k - 1; i++)
         sum = sum + L(k, i)*x(i);
      x(k) = (b(k) - sum)/L(k, k);
   }

   return x;
}


And the error at runtime is:
rich@rich-VirtualBox:~/workspace/linalgtest/Debug$ ./linalgtest
linalgtest: /usr/include/eigen3/Eigen/src/Core/DenseCoeffsBase.h:407: Eigen::DenseCoeffsBase<Derived, 1>::Scalar& Eigen::DenseCoeffsBase<Derived, 1>::operator()(Eigen::DenseCoeffsBase<Derived, 1>::Index) [with Derived = Eigen::Matrix<double, -0x00000000000000001, 1>, Eigen::DenseCoeffsBase<Derived, 1>::Scalar = double, Eigen::DenseCoeffsBase<Derived, 1>::Index = long int]: Assertion `index >= 0 && index < size()' failed.
Aborted (core dumped)
jitseniesen
Registered Member
Posts
204
Karma
2
You need to specify the size of the vector x. If you declare x as you did, it will be an empty vector (a vector of length 0), so you get an error when you try to access its first element.


Bookmarks



Who is online

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