1

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! :)

chocobo_ff
  • 510
  • 1
  • 5
  • 14

1 Answers1

1

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;
     }
};
Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • `someClass() : L(4) ...` throws an error, but `L = new ublas::vector(4)` works. I'll look further into member objects and see if I can get it to work. Thanks! – chocobo_ff May 10 '12 at 20:51
  • OK I see what I have done wrong now, many thanks for the example :) – chocobo_ff May 10 '12 at 21:15