Jump to content
  • Advertisement
Sign in to follow this  
vish40

problem with constructors in C++

This topic is 4993 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!