Boost logo

Boost Users :

Subject: Re: [Boost-users] Error (runtime) assigning multi_array to multi_array with smaller dimensions
From: Oei, YC (oei.yungchin_at_[hidden])
Date: 2009-05-21 20:44:24


> //class header file
> boost::multi_array<char, 2> array2d;
>
> //class cpp file
> //at class constructor
> array2d = boost::multi_array<char,2>(boost::extents[x][y]);

I think the problem is that the last line quoted above is an
assignment, not an initialisation: if you look in the boost headers,
you'll see the assignment operator is very different from the copy
constructor.

Basically if you have a constructor that first default-constructs
array2d, then you'll have an empty array. Then assigning to it, means
you are trying a deep-copy from a x-by-y array to a 0-by-0 array.

One way to fix it is to add a line:
//at class constructor
array2d.resize(boost::extents[x][y]);
array2d = boost::multi_array<char,2>(boost::extents[x][y]);

Otherwise, you can initialise array2d before the constructor body.

Yung-Chin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net