Getter for dynamic array in c++ not working

381 Views Asked by At

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;
}
3

There are 3 best solutions below

0
On BEST ANSWER

The getter is working but you didn't initialize the member.

    array3i test_array_(extents[x][y][z]);

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 initialize test_array_ shape in the constructor initializer list).

Live On Coliru

Fixed:

#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 const x, int const y, int const z)
    {
        test_array_.resize(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()
{
    Test test(2, 3, 5);

    cout << "Size from getter: " << test.test_array().size() << endl;
}
0
On

The problem is probably that you have two test_array_ variables: One in the class as a class member, one local in the init function.

The local variables shadows, and overrides, the member variable.

2
On

In your init function you should be having:

void init(int const x, int const y, int const z)
{
    //test_array_.resize(extents[x][y][z]);
    test_array_ = array3i(extents[x][y][x]); 
    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;
            }
        }
    }
}