In that case, step through with a debugger.
Printable View
In that case, step through with a debugger.
"An access violation (Segmentation fault) raised in your program"
Mean anything to anyone?
Since you don't initialize numOfRows or numOfCols, they have random values. Thus, you're accessing memory that doesn't belong to you in the loop.Code:TicTacToe:: TicTacToe ()
{
int numOfRows;
int numOfCols;
// Initialising TicTacToe //
for (int r = 0; r < numOfRows; r++) //looping rows
for (int c = 0; c < numOfCols; c++) //looping columns
board[r][c] = ' ';
}
Replace all other usages of the number 3 as appropriate.Code:const int NUM_ROWS = 3;
const int NUM_COLS = 3;
...
char board [NUM_ROWS] [NUM_COLS];
...
TicTacToe:: TicTacToe ()
{
// Initialising TicTacToe //
for (int r = 0; r < NUM_ROWS; r++) //looping rows
for (int c = 0; c < NUM_COLS; c++) //looping columns
board[r][c] = ' ';
}
Sorry to be a pain!
But another error - just when you think you are sorted:
- ISO C++ forbids initialization of member `NUM_ROWS'
Here
const int NUM_ROWS = 3;
and
ISO C++ forbids initialization of member `NUM_COLS'
const int NUM_COLS = 3;
Any ideas on these errors? Tried googling for these errors, but to no avail?Code:using namespace std;
#include <iomanip>
class TicTacToe
{
private:
enum Status {WIN, DRAW, CONTINUE };
const int NUM_ROWS = 3;
const int NUM_COLS = 3;
public:
TicTacToe();
void makeMove ();
void printBoard ();
bool validMove (int r, int c);
bool xoMove (int input);
Status gameStatus ();
void holdscreen ();
char board [NUM_ROWS] [NUM_COLS];
};
TicTacToe:: TicTacToe ()
{
// Initialising TicTacToe //
for (int r = 0; r < NUM_ROWS; r++) //looping rows
for (int c = 0; c < NUM_COLS; c++) //looping columns
board[r][c] = ' ';
}
Put a static before them.
That's got it - cheers for your help. Before tried deleting const and adding static, not putting static before const
Thanks again
If you want to declare those members as const... You can't initialize data members in that manner, they need to be initialized in the class' constructor. And for const data members, you need to use an initialization list.Quote:
Originally Posted by rebel
Code:class TicTacToe
{
private:
enum Status {WIN, DRAW, CONTINUE };
const int NUM_ROWS;
const int NUM_COLS;
public:
TicTacToe();
void makeMove ();
void printBoard ();
bool validMove (int r, int c);
bool xoMove (int input);
Status gameStatus ();
void holdscreen ();
char board [NUM_ROWS] [NUM_COLS];
};
TicTacToe:: TicTacToe () : NUM_COLS(3), NUM_ROWS(3)
{
// Initialising TicTacToe //
for (int r = 0; r < NUM_ROWS; r++) //looping rows
for (int c = 0; c < NUM_COLS; c++) //looping columns
board[r][c] = ' ';
}
Considering that his code is bound to a board size of 3 (gameStatus assumes this), it doesn't make sense to store the board size constants per object.