My Connect4 class wont print out the board. Help?

****main****
#include "Connect4.hpp"

int main()
{
Connect4 player1;
player1.DrawBoard();

return 0;
}

****class****

#include <iostream>
using namespace std;

class Connect4
{
private:
int rows;
int columns;
double * * board;
char piece;


public:
Connect4()
{
int rows = 6;
int columns = 7;
board = new double*[rows];
for (int i = 0; i < rows; i++)
{
board[i] = new double[columns];
}

}

// Connect4(int numRows, numColumns)
// {
// rows = numRows;
// columns = numColumns;
// board = new double*[rows];
// for (int i = 0; i < rows; i++)
// {
// board[i] = new double[columns];
// }
// }

void DrawBoard()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
board[i][j] = 0; //initialize the board to 0
}
}

//print contents of the board
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << board[i][j] << " " ;
}
cout << endl;
}

}

};
You have not set rows or columns in the Connect4 object!

You may think you have ... but the lines
1
2
int rows = 6;
int columns = 7;

in the constructor actually declare new local variables rows and columns which hide the data members of the class.

Simply remove the types here and write
1
2
rows = 6;
columns = 7;


Then you will be setting the class members, not local variables.

Please use code tags when posting code. It makes it easier to see code structure from indentation and allows us to point to line numbers.
Last edited on
Wow, such a simple mistake. Thank you so much!
Topic archived. No new replies allowed.