problem with constructors in C++

Started by
7 comments, last by vish40 19 years, 5 months ago
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.
Advertisement
are yuo ever initializing nRows and/or nCols?
[size="2"]I like the Walrus best.
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;
}
}
What is the probelm? does it not compile?
Don't shoot! I'm with the science team.....
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.
ignore the numbers i put them in there.
> Completely stupid answer removed from here <
-=[ J ]=-I always forget to change the tagline.
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.
Thanks a lot jjmontes that fixed the problem.

This topic is closed to new replies.

Advertisement