-2

I have a tic tac toe game worked up here, but i keep getting these errors:

[Error] expected declaration before '}' token

[Error] expected unqualified-id before numeric constant

[Error] expected '}' before numeric constant

[Error] expected identifier before numeric constant

#include <iostream> 
#include <string> 
using std::string; 

enum SquareState{ blank = ' ', X = 'X', 0 = '0'};

class Board{
    private: 
        const int HEIGHT;
        const int WIDTH;
        int* gboard;
    public:
        Board() : WIDTH(3), HEIGHT(3)
        {
            gboard = new int[9];
            for (int i = 0; i < 9; i++)
                *(gboard+i) = blank;
        }

        Board() {delete[] gboard;}
        void setX(int h, int w);
        void set0(int h, int w);
        bool isTaken(int h, int w);
        SquareState isLine();
        void draw();
};

void Board::setX(int h, int w)
{
    *(gboard + h*HEIGHT + w) = X;
}
void Board::set0(int h, int w)
{
    *(gboard + h*HEIGHT + w) = 0;
}
bool Board::IsTaken(int h, int w)
{
    return *(gboard + h*HEIGHT + w) != ' ';
}
SquareState Board::IsLine()
{
    if(*gboard==X && *(gboard +1)==X && *(gboard +2)==X)
        return X;
    if(*gboard==0 && *(gboard +1)==0 && *(gboard +2)==0)
        return 0;
    if(*(gboard+3)==X && *(gboard +4)==X && *(gboard +5)==X)
        return X;
    if(*(gboard+3)==0 && *(gboard +4)==0 && *(gboard +5)==0)
        return 0;
    if(*(gboard+6)==X && *(gboard +7)==X && *(gboard +8)==X)
        return X;
    if(*(gboard+6)==0 && *(gboard +7)==0 && *(gboard +8)==0)
        return 0;
    if(*gboard==X && *(gboard +3)==X && *(gboard +6)==X)
        return X;
    if(*gboard==0 && *(gboard +3)==0 && *(gboard +6)==0)
        return 0;
    if(*(gboard+1)==X && *(gboard +4)==X && *(gboard +7)==X)
        return X;
    if(*(gboard+1)==0 && *(gboard +4)==0 && *(gboard +7)==0)
        return 0;
    if(*(gboard+2)==X && *(gboard +5)==X && *(gboard +8)==X)
        return X;
    if(*(gboard+2)==0 && *(gboard +5)==0 && *(gboard +8)==0)
        return 0;
    if(*gboard==X && *(gboard +4)==X && *(gboard +8)==X)
        return X;   
    if(*gboard==0 && *(gboard +4)==0 && *(gboard +8)==0)
        return 0;
    if(*(gboard+2)==X && *(gboard +4)==X && *(gboard +6)==X)
        return X;
    if(*(gboard+2)==0 && *(gboard +4)==0 && *(gboard +6)==0)
        return 0;

    return blank;
}
void Board::draw()
{
    using std::cout;
cout << "\n"
for (int i = 0; i <HEIGHT; i++){
    cout << (char)*(gameboard + i*HEIGHT);
    for (int c = 1; c < WIDTH; c++);
        cout << " | " << (char)*(gameboard + i*WIDTH + c);
    cout << "\n" << "------" << "/n";
}
class Game
{
    public:
        Board* doInput(string player, gboard * gb);
        bool inRange (int test);
};

Board* Game::doInput(string player, Board* gb)
{
    using std::cout;
    using std::cin;

    gb->draw();
    string letter;
    if(player.compare("one")==0)
        letter = "X";
    else if (player.compare("two")==0)
        letter = "0";
    else return gb;

    int input1, input2;
    do{
        do{
            cout << "\nplayer" << player.c_str()
                << ", choose a row to put an "
                << letter.c_str() << ": ";
            cin >> input1;
        }while(!inRange(input1));
        do{
            cout << "\nplayer" << player.c_str()
                << ", choose a column to put an "
                << letter.c_str() << ": ";
            cin >> input2;  
        }while(!inRange(input2));
    }while (gb->isTaken(input1, input2));

    if (player.compare("one")==0)
        gb->setX(input1, input2);
    else
        gb->set0(input1, input2);
    return gb;
}

bool Game::inRange(int test)
{
    return test > -1 && test < 3;
}

int main(void)
{
    using std::cout;
    using std::cin;

    Board* gb = new Board;
    Game g;
    string player1, player2;
    cout<< "Let's play some Triple-T"
        <<"\nplayer one, introduce yourself: ";
    cin >> player1;
    cout << "\nplayer two, introduce yourself: ";
    cin >> player2;

    while (gb->isLine()== ' ')
    {
        gb = g.doInput("one", gb);
        gb = g.doInput("two", gb);
    } 
    gb->draw();
    if (gb->isLine()==X)
        cout<< "\nplayer one has prevailed\n";
    else 
        cout<< "\nplayer one has prevailed\n";
    return 0;
}//end main
  • There are too many fundamental errors. Why not try something shorter and simpler first (after learning the basics of the language?) – juanchopanza Mar 17 '14 at 16:57
  • Why did you turn a perfectly valid and safely copyable board into one that isn't? `gboard = new int[9];` Why not just declare an int array with 9 elements? By introducing dynamically allocated memory, you took on a whole lot more than you bargained for. For example, you now need to implement "the rule of three", i.e. copy constructor, assignment operator, (plus destructor), to make this class usable in say, an application that maintains more than one tic-tac-toe game in a container. You also have this in main(): `Board* gb = new Board;` All you need is this: `Board gb;` – PaulMcKenzie Mar 17 '14 at 17:00

4 Answers4

3

You are missing ; in cout << "\n" in void Board::draw()

And in your enum, the variable name can't be a keyword/constant - 0

brokenfoot
  • 11,083
  • 10
  • 59
  • 80
0
enum SquareState{ blank = ' ', X = 'X', 0 = '0'};

Using 0 as a constant identifier? I'm not sure that's possible.

Simon Bosley
  • 1,114
  • 3
  • 18
  • 41
0

The error is here :

enum SquareState{ blank = ' ', X = 'X', 0 = '0'};
                                        ^

Names for enum values have to follow the same rules as names for variables - you can't have a name begining with digits and consisting only of digits.

Losiowaty
  • 7,911
  • 2
  • 32
  • 47
  • That's what I thought too. Fixed that, now I'm getting [Error] 'Board::Board()' cannot be overloaded [Error] uninitialized member 'Board::HEIGHT' with 'const' type 'const int' [-fpermissive] (same error for Board::WIDTH. how do i initialize these?) and it's telling me SquareState::isLine and bool Board::isTaken are nt declared in class 'Board' – I Can't Advise You Counselor Mar 17 '14 at 18:38
  • You cannot have two default constructors - from the looks of it, it seems like the second one was meant to be a destructor : `~Board()`. – Losiowaty Mar 17 '14 at 21:43
0

enum SquareState{ blank = ' ', X = 'X', 0 = '0'};

the 0 = '0' appears to be the culprit, as 0 is not a valid variable name.

Chrisji
  • 311
  • 2
  • 13