7

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?

2 Answers 2

8

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 ] ) {
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.