S
spoordp
I'm rather new to C++, but I'm quickly learning. Anytime I don't know
why something happens, I have to find out. Here is one that has
stumped me. I wanted to declare an array with a constant variable in
my class declaration. The only way I could figure this out is to use
enum( I was using MvC++6.0). I made an enum value in my header. Here
is the code.
// TicTacToe.h
#ifndef TICTACTOE_H
#define TICTACTOE_H
class TicTacToe {
private :
// enum { MAXBOARDSIZE = 3};
enum {PlAYERONE = 1, PLAYERTWO = 2, MAXBOARDSIZE = 3};
int board[MAXBOARDSIZE][MAXBOARDSIZE];
public:
TicTacToe();
void setPosition(int, int, int);
int getBoard(int, int);
void printBoard();
void startgame(int);
int getBoardSize();
};
#endif
and here is my .cpp file
// TicTacToe.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "TicTacToe.h"
TicTacToe::TicTacToe()
{
memset(board, 0, sizeof(board));
cout << board [0][0] << endl;
cout << board [2][0] << endl;
}
int TicTacToe::getBoard(int x, int y)
{
return board [x][y];
}
void TicTacToe:rintBoard()
{
for (int counter = 0; counter <MAXBOARDSIZE; counter++)
{
for (int counter2 = 0; counter2 < MAXBOARDSIZE; counter2++)
cout << board[counter][counter2] << " ";
cout << endl;
}
cout << " player is : " << PLAYERONE << "board size is : " <<
MAXBOARDSIZE <<endl;
}
I decided to cout the value of each of these just to make sure it would
work. Here's the weird part that stumps me. I could compile when
PLAYERONE was in that last cout line and it would output PLAYERONE and
MAXBOARDSIZE just fine. When I erased the last three letters of
playerone and put in TWO, the compiler would give me an undeclared
identifier. BUT.... if I went back to the header file and just 'control
c'ed PLAYERTWO in the enum, and repasted it in the cout call, it would
compile and display just fine!!! Then of course the same thing would
happen to PLAYERONE until I copied and pasted. All the while the
compiler never balked about MAXBOARDSIZE. I've tried this on both
MVc++6 and dev c++ latest version. Why is this????
why something happens, I have to find out. Here is one that has
stumped me. I wanted to declare an array with a constant variable in
my class declaration. The only way I could figure this out is to use
enum( I was using MvC++6.0). I made an enum value in my header. Here
is the code.
// TicTacToe.h
#ifndef TICTACTOE_H
#define TICTACTOE_H
class TicTacToe {
private :
// enum { MAXBOARDSIZE = 3};
enum {PlAYERONE = 1, PLAYERTWO = 2, MAXBOARDSIZE = 3};
int board[MAXBOARDSIZE][MAXBOARDSIZE];
public:
TicTacToe();
void setPosition(int, int, int);
int getBoard(int, int);
void printBoard();
void startgame(int);
int getBoardSize();
};
#endif
and here is my .cpp file
// TicTacToe.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "TicTacToe.h"
TicTacToe::TicTacToe()
{
memset(board, 0, sizeof(board));
cout << board [0][0] << endl;
cout << board [2][0] << endl;
}
int TicTacToe::getBoard(int x, int y)
{
return board [x][y];
}
void TicTacToe:rintBoard()
{
for (int counter = 0; counter <MAXBOARDSIZE; counter++)
{
for (int counter2 = 0; counter2 < MAXBOARDSIZE; counter2++)
cout << board[counter][counter2] << " ";
cout << endl;
}
cout << " player is : " << PLAYERONE << "board size is : " <<
MAXBOARDSIZE <<endl;
}
I decided to cout the value of each of these just to make sure it would
work. Here's the weird part that stumps me. I could compile when
PLAYERONE was in that last cout line and it would output PLAYERONE and
MAXBOARDSIZE just fine. When I erased the last three letters of
playerone and put in TWO, the compiler would give me an undeclared
identifier. BUT.... if I went back to the header file and just 'control
c'ed PLAYERTWO in the enum, and repasted it in the cout call, it would
compile and display just fine!!! Then of course the same thing would
happen to PLAYERONE until I copied and pasted. All the while the
compiler never balked about MAXBOARDSIZE. I've tried this on both
MVc++6 and dev c++ latest version. Why is this????