chocobo_ff
chocobo_ff

Reputation: 560

ublas vector pointer

I'm trying to initialise/assign values to a ublas vector by defining the vector in my .h file:

someClass{
    ublas::vector<double> *L;
} ;

Then in my .cpp file:

someClass::someClass()
{
    L = new ublas::vector<double>[4];
    L->data()[0] = 1;
}

The code compiles fine, but when I run it, the last line in my .cpp file gives an error. There is probably something very simple I'm missing but I can't figure out what...

Many thanks in advance! :)

Upvotes: 1

Views: 331

Answers (1)

Cubbi
Cubbi

Reputation: 47428

You've created an array of four ublas::vectors, each of size zero. Assigning to the first element of an empty vector throws an exception.

Did you mean to write

L = new ublas::vector<double>(4); 

instead?

Not to mention that the use of new and pointer is questionable, for most uses, a member object is more appropriate:

class someClass {
    ublas::vector<double> L;
 public:
     someClass() : L(4) {
          L[0] = 1;
     }
};

Upvotes: 1

Related Questions