enum creates undeclared identifier (only sometimes?)

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::printBoard()
{
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????
 
P

Pete Becker

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????

Read those names carefully everywhere they're used, and remember that
C++ is case-sensitive.
 
G

golfprowanabe

Those names are all caps!!! I paid particular attention the the
spelling as well. Can anyone compile this and confirm this issue?
 
P

Pete Becker

golfprowanabe said:
Those names are all caps!!! I paid particular attention the the
spelling as well. Can anyone compile this and confirm this issue?

I have no doubt that you get this error, because I spotted the cause and
told you what to do to find it.
 
G

golfprowanabe

Pete I guess you lost me. "Read those names carefully everywhere
they're used, and remember that C++ is case-sensitive." I can't find
any problem. I can copy and paste "PLAYERONE", then delete the "ONE"
and add "TWO" and it will not compile. I guess I just don't understand
what is going on.
 
G

golfprowanabe

I SEE IT!!! I SEE IT!!! Time for me to go to bed!!! Funny now I see
Kai-Uwe's reply after I found it! Thanks Pete for giving me the subtle
hint because then I knew it had to be something simple stupid! I
looked that constant over several times. Wow. Definately time for
bed.
 
J

John Carson

golfprowanabe said:
I SEE IT!!! I SEE IT!!! Time for me to go to bed!!! Funny now I
see Kai-Uwe's reply after I found it! Thanks Pete for giving me the
subtle hint because then I knew it had to be something simple stupid!
I looked that constant over several times. Wow. Definately time for
bed.

This sort of stuff happens all the time when you are beginning with C++,
even when well-rested. As time passes, you still make the same mistakes, but
you get quicker at finding them.
 
P

Pete Becker

golfprowanabe said:
I SEE IT!!! I SEE IT!!! Time for me to go to bed!!! Funny now I see
Kai-Uwe's reply after I found it! Thanks Pete for giving me the subtle
hint because then I knew it had to be something simple stupid! I
looked that constant over several times. Wow. Definately time for
bed.

That often helps. <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top