tags:

views:

158

answers:

2

Hi to all,

for start i would like to say that i am newbie.

i am trying to initialized boost:multi_array inside my class. I know how to create a boost:multi_array:

 boost::multi_array<int,1> foo ( boost::extents[1000] );

but as part of a class i have problems:

 class Influx {
   public:
     Influx ( uint32_t num_elements );
     boost::multi_array<int,1> foo;

   private:

 };

 Influx::Influx ( uint32_t num_elements ) {
    foo = boost::multi_array<int,1> ( boost::extents[ num_elements ] );
 }

my program pass compilation but during run-time i get an error when i try to accuse an element from foo (e.g. foo[0]).

since i am newbie, don't really know how to solve this problem.

regards

+4  A: 

Use an initialisation list (BTW, I know zip about this bit of Boost, so I'm going by your code):

Influx::Influx ( uint32_t num_elements ) 
   : foo( boost::extents[ num_elements ] ) {
}
anon
thanks, it solved the problem
Eagle
Make sure you understand why. Also, accept the answer by clicking the green check mark. :)
GMan
... so that other people see that the problem is solved.
Georg Fritzsche
+1  A: 

If you move things around so that the multi-array object gets created with the paramater:

#include "boost/multi_array.hpp"
#include <iostream>

class Influx {
public:
    Influx ( unsigned int num_elements ) :
        foo( boost::extents[ num_elements ] )
    {
    }
    boost::multi_array<int,1> foo;
 };

int main(int argc, char* argv[])
{
    Influx influx(10);
    influx.foo[3] = 5;
    int val = influx.foo[3];
    std::cout << "Contents of influx.foo[3]:" << val << std::endl;
    return 0;
}

I think what was happening for you is that you created foo when you Influx object was created, but then later on you set it again, so when people call it, bad things happen.

I was able to get the above code working on MS VS 2008.

Bill Patel