Sign in to follow this  

problem with constructors in C++

This topic is 4781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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;
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this