Reputation: 97
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.
Upvotes: 0
Views: 463
Reputation: 32797
You have mainly two problems:
Problem - 1: You have declared an array inside your constructor, which make not initialize your class member. Therefore, when you use your getterFunction()
, you are accessing uninitialized array element, which gives some garbage values.
Problem - 2: You have mentioned a parameterized constructor here,
//constructor
genericClassName::genericClassName(bool)
{ .... } ^^^^^^
and created the instance without passing a parameter
//creation of an object of the class
genericClassName objectName; // this need a default constructor
^^^^^^^^^^^^
If this was all your code, I don't think that, the code will compile as you have not defined any default constructor
in your code.
The non-generic way of fixing is as follows: SEE HERE
class genericClassName
{
private:
bool genericArray [3][3]; // this has be defined here/ not generic anymore
public:
genericClassName()
{
for (auto &row : genericArray)
for (auto& ele: row)
ele = false;
}
genericClassName(bool trueOrfalse)
{
for (auto &row : genericArray)
for (auto& ele: row)
ele = trueOrfalse;
}
.............
.............
In order to make the 2D array generic, you can use std::vector
. That will look like as follows:
#include <iostream>
#include <vector>
using Type2D = std::vector< std::vector<bool> >;
class genericClassName
{
private:
Type2D genericArray;
public:
genericClassName() // defualt constrctor with some random size
:genericArray(3, std::vector<bool>(3, false))
{}
genericClassName(bool trueOrfalse, int size) // use initilizer list here
:genericArray(size, std::vector<bool>(size, trueOrfalse))
{}
bool getterFunction (int row, int column)
{ return genericArray[row][column]; }
// just to print all elements
friend std::ostream& operator<<(std::ostream& out, const genericClassName& obj);
};
std::ostream& operator<<(std::ostream& out, const genericClassName& obj)
{
for (const auto &row : obj.genericArray)
for (const auto& ele: row)
out << ele << " ";
return out;
}
int main(void)
{
genericClassName objectName2; // works with default constrctor
std::cout << objectName2.getterFunction(1, 1) << std::endl;
std::cout << objectName2 << std::endl;
genericClassName objectName(true, 4); // specify the size of your 2D array as well
std::cout << objectName.getterFunction(1, 1) << std::endl;
std::cout << objectName << std::endl;
return 0;
}
Upvotes: 1
Reputation: 958
Here is the working code. The problem is with assigning bool values to 2 dim array at constructor.
#include <iostream>
#include <cstdlib>
using namespace std;
class genericClassName{
bool genericArray [3][3];
public:
genericClassName()
{
int i, j;
for( i=0;i<3;i++)
for(j=0;j<3;j++)
genericArray[i][i] = false;
}
bool getterFunction (int row, int column)
{
return genericArray[row][column];
}
};
int
main(void)
{
genericClassName objectName;
cout << objectName.getterFunction (1, 1) << endl;
return 0;
}
Upvotes: 0