In that case, step through with a debugger.
In that case, step through with a debugger.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
"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] = ' '; }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.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] = ' '; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law