2

Suppose i need a five-dimensional array as class member and want to use it in different functions. For this puropose I use boost::multi_array e.g.:

class myClass {

typedef boost::multiarray<double, 5> fiveDim;
typedef fiveDim:: index index;

void init(){
fiveDim myArray(boost::extents[3][3][3][3][3]);
// I can use myArray in this scope
}

void printArray(){
// myArray not available here
}

Now, because of the function scope, i can clearly not use myArray in printArray(), but i also can't initialize the array directly in the class scope, outside a function (after the two typedefs.)

How do i define the array so that every class function is able to use it? The dimensions are known at compile time and always the same.

6
  • That's not a class member, that's a local function variable. Make it an actual member variable, and then initialize it in a constructor. Commented Mar 19, 2017 at 20:26
  • if I understand you correct you just want to make myArray member variable of myClass. And after it the myArray variable will be available from myClass member functions. Check this code - coliru.stacked-crooked.com/a/520b01fd62e63ec0 Commented Mar 19, 2017 at 20:31
  • 1
    Do you know how to make an int class member? What are the principal difficulties of replacing int with the multi-array type? Commented Mar 19, 2017 at 20:39
  • thanks, that solved it. for understanding, what does ": yourArray(boost::extents[3][3][3][3][3])" actually do in this case? Commented Mar 19, 2017 at 21:09
  • @n.m. the problem was that i kept getting compiler errors and runtime Assertion fails within the boost library. Commented Mar 19, 2017 at 21:11

1 Answer 1

0

Maybe you can use this:

#include <iostream>
#include <string>
#include <boost/multi_array.hpp>
#include <boost/array.hpp>
#include <boost/cstdlib.hpp>

namespace{
    constexpr size_t dim1_size = 3;
    constexpr size_t dim2_size = 3;
    constexpr size_t dim3_size = 3;
    constexpr size_t dim4_size = 3;
    constexpr size_t dim5_size = 3;
    
    void print(std::ostream& os, const boost::multi_array<double, 5>& a){
        os<<"implementation of print: "<<a[0][0][0][0][0]<<std::endl;
    }
    
    boost::multi_array<double, 5> createArray(){
        return boost::multi_array<double, 5>(boost::extents[dim1_size][dim2_size][dim3_size][dim4_size][dim5_size]);
    }
}

class myClass {

public:
    boost::multi_array<double, 5> myArray = createArray();

    void printArray(){
        print(std::cout, myArray);
    }
};

int main()
{
  myClass a;
  a.myArray[0][0][0][0][0] = 42;
  a.printArray();
}

To pass a boost::multi_array in a function you can use references like in print. To create a boost::multi_array you must return it as copy. The compiler may optimize it to a move.

Sign up to request clarification or add additional context in comments.

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.