I am struggling to store a 2D array of integers in a private variable after it has been passed to the constructor of class.
Header file:
class SomeClass {
public:
SomeClass(int myArray[][2], int myArraySize);
private:
int _myArray[][2];
int _myArraySize;
};
Class constructor in .cpp file:
SomeClass::SomeClass(int myArray[][2], int myArraySize) {
int _stateMap[myArraySize][2] = myArray[myArraySize][2]; // various permutations of this don't work
}
I can pass the 2D array okay and direct references to myArray work correctly.
But all my attempts to assign the array to a private variable (_myArray) for future use either won't compile or outputs random numbers when accessed.
What are you doing right would be easier to answer. Nothing.
You are creating a local variable in the constructor. That variable will go out of scope as soon as the constructor ends.
You have a private variable that is not sized correctly. You can't do that.
You are trying to assign a value to the first element beyond the bounds of the array that is coming from the first element beyond the bounds of the input array.
If you insist on a variable sized array, it MIST be allocated using malloc().