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 passes through compilation but during run-time I get an error when I try to accuse an element from foo
(e.g. foo[0]
).
How to solve this problem?
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 ] ) {
}
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.