problem with constructors in C++
Ok guys, this is really pissing me off.
I'm trying to initialize my multi-dimensional array with some random characters in the default constructor (dont know if this is correct declaration). This is how i did it:
Board::Board()
{
for(int j = 0; j < nRows; j++)
{
for(int k = 0; k < nCols; k++)
{
board_array[j][k] = 0;
}
}
}
This is my Board Class:
class Board
{
private:
char board_array[8][8]; // multidimensional array that will contain the random characters
int nRows;
int nCols;
public:
Board(); // initialize the array with the random character
void cheat(ostream& out); // function that will output the answers to a file
void print_unflipped(); // prints the board to the screen
void print_flipped(int, int, int, int);
void setIfMatch(int, int, int, int);
// accessor functions
int getnRows;
int getnCols;
// mutators functions
void setnRows(int);
void setnCols(int);
};
Can anyone tell what I'm doing wrong here. Any help would be greatly appericaited.
Ya, this is the code for that:
void Board::setnRows(int a)
{
if((a > 0) && (a <= 8))
{
nRows = a;
}
}
void Board::setnCols(int b)
{
if((b > 0) && (b <= 8))
{
nCols = b;
}
}
void Board::setnRows(int a)
{
if((a > 0) && (a <= 8))
{
nRows = a;
}
}
void Board::setnCols(int b)
{
if((b > 0) && (b <= 8))
{
nCols = b;
}
}
no, it compiles and runs fine and stuff.
But, the array doesn't get initialize for some reason.
This is what it the array looks like
1 2 3 4
1 Ì Ì Ì Ì
2 Ì Ì Ì Ì
3 Ì Ì Ì Ì
4 Ì Ì Ì Ì
instead of the I it should have random characters.
But, the array doesn't get initialize for some reason.
This is what it the array looks like
1 2 3 4
1 Ì Ì Ì Ì
2 Ì Ì Ì Ì
3 Ì Ì Ì Ì
4 Ì Ì Ì Ì
instead of the I it should have random characters.
You will need to give values for nRows and nCols. It is no good to be using set methods when you need the values in the constructor i.e. before you can use the set methods.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement