vendredi 8 juin 2018

Getter function to retrieve a 2d array element when the 2d array is a data member of a class

I have a 2d array which is a data member of a class. I initialized it to false.

It is in its own .cpp class file.

Format is:

//constructor
genericClassName::genericClassName(bool)
{

bool genericArray [3][3] = 
{
    { false, false, false },
    { false, false, false },
    { false, false, false },
};

From my main method I have used a getter function in the following format:

//creation of an object of the class
genericClassName objectName;

//console output
std::cout << objectName.getterFunction (1, 1) << std::endl;

The numbers in the () are the arguments passed to the getterFunction to represent the subscripts of the 2d array. Row and column in that order.

The getterFunction definition in the class .cpp file is in the following format:

bool genericClassName::getterFunction (int row, int column)
{
    return genericArray[row][column];
}

The function is not returning 0. Instead I get a three digit number. I am uncertain if the function call is wrong, the initialization of the array is wrong, or the definition of the getter is wrong. Is the definition creating a local array which is not the array initialized to false?

The getter is public. The array is private.

Aucun commentaire:

Enregistrer un commentaire