I am using a dynamic array from boost in one of my classes and having trouble to write a proper getter function for it. Heres what I tried (I checked the size of the array within the class setter and with the getter from the main function):
#include <iostream>
#include "boost/multi_array.hpp"
using namespace std;
using namespace boost;
typedef multi_array<int, 3> array3i;
class Test {
private:
array3i test_array_;
void init(int x, int y, int z) {
array3i test_array_(extents[x][y][z]);
cout << "Size should be: " << test_array_.size() << endl;
for (int j=0; j<x; j++) {
for (int jj=0; jj<y; jj++) {
for (int jjj=0; jjj<z; jjj++) {
test_array_[j][jj][jjj] = j+jj+jjj;
}
}
}
};
public:
array3i test_array() {return test_array_;};
Test(int x, int y, int z) {
init(x, y, z);
};
};
int main(int argc, const char * argv[]) {
Test test(2,3,5);
cout << "Size from getter: " << test.test_array().size() << endl;
return 0;
}
The getter is working but you didn't initialize the member.
initializes a local variable (that stops to exist after
init()
exits).The problematic part was (likely) that you can't just assign a multi_array of different size/shape. So you need to use
resize()
(or initializetest_array_
shape in the constructor initializer list).Live On Coliru
Fixed: