Constructor problem

This is a discussion on Constructor problem within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, With my TicTac Toe game i'm creating I think i'm nearly there, but have a problem with my ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    Constructor problem

    Hi Guys,

    With my TicTac Toe game i'm creating I think i'm nearly there, but have a problem with my constructor.

    When I try and run my program I get the message:

    return type specification for constructor invalid

    in this line:

    Code:
     int TicTacToe(); //in public
    And also another message saying

    in function int main () // in CPP file

    expected primary-expression before "int"

    in these lines:

    Code:
    game.validMove(int r, int c);
    Code:
    game.xoMove(int symbol);
    Here's my full code, can anyone please advise?:

    Thanks

    TicTacToe.h
    Code:
    // TICTACTOE.H
    // A Program to run the Tic-Tac-Toe Program
    #include <iostream>
    using namespace std;
    #include <iomanip>
    using namespace std;
    class TicTacToe
    {
       private:
         enum Status {WIN, DRAW, CONTINUE };
         char board [3] [3];
       public:
         int TicTacToe();
         void makeMove ();
         void printBoard ();
         bool validMove (int r, int c);
         bool xoMove (int input);
         Status gameStatus ();
         void holdscreen ();
    };
     
    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] = ' ';
    }
    
    bool TicTacToe::validMove (int r, int c)
    {
    
    // Validate Move Start for X //
    cout << "Enter coordinates for X: ";
        cin >> r;
        while(true) {
        if ((r>=3) || (r<0))
        cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
    else break;}
    // ** End Validate Move Start for X ** //
     
    // Validate Move Start for O //
    cout << "Enter coordinates for O: ";
        cin >> c;
        while(true) {
        if ((c>=3) || (c<0))
        cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
    else break;}
    // ** End Validate Move Start for O ** //
    
    }
    
    TicTacToe:: Status TicTacToe:: gameStatus(void)
    {
      int a;
      int r;
      int c;
      
      // Check for win in diagonals //
      if (board [0][0]!= '_' && board [0][0] == board [1][1] 
      && board [0][0] == board [2][2])
      return WIN;
      else if (board [0][2]!= '_' && board [0][2] == board [1][1] 
      && board [0][2] == board [2][0])
      return WIN;
      //** End Check for win in diagonals ** //
    
      // Check for win in rows
      if (board [0][0]!= '_' && board [0][0] == board [1][0] 
      && board [0][0] == board [2][0])
      return WIN;
        else if (board [0][1]!= '_' && board [0][1] == board [1][1] 
      && board [0][1] == board [2][2])
      return WIN; 
        else if (board [0][2]!= '_' && board [0][2] == board [1][2] 
      && board [0][2] == board [2][2])
      return WIN; 
      //** End Check for win in rows  **//  
      
      // Check for win in columns
      if (board [0][0]!= '_' && board [0][0] == board [0][1] 
      && board [0][0] == board [0][2])
      return WIN;
        else if (board [1][0]!= '_' && board [1][0] == board [1][1] 
      && board [1][0] == board [1][2])
      return WIN;
       else if (board [2][0]!= '_' && board [2][0] == board [2][1] 
      && board [2][0] == board [2][2])
      return WIN; 
      // ** End Check for win in columns ** //
    }  
    void TicTacToe::printBoard (void)
    {
         cout << "   0   1   2\n\n";
         
         for ( int r=0; r < 3; ++r ) {
             cout << r;
             
             for ( int c=0; c < 3; ++c ) {
                 cout << setw (3) << static_cast < char > ( board [r] [c] );
                 
                 if (c !=2 )
                    cout << " |";
             }
             
             if (r != 2)
                cout << "\n ______|______|______"
                     << "\n ______|______|______\n";
             }
         
         cout <<"\n\n";
    }    
    void  TicTacToe::makeMove(void)
    {
       printBoard();
       
       while ( true ) {
          if (xoMove ( 'X' ) )
             break;
       else if (xoMove ( 'O' ) )
             break;
       }
    }
    
    bool TicTacToe::xoMove (int symbol)
    {
       int x,y;
       
       do {
           cout << "Player" << static_cast < char > (symbol) << " enter move: ";
           cin >> x >> y;
           cout << '\n';
       } while ( !validMove ( x, y ) );
       
       board [x] [y] = symbol;
       printBoard();
       Status xoStatus = gameStatus();
       
       if ( xoStatus == WIN ) {
          cout << "Player " << static_cast < char > (symbol ) << " wins!\n";
          return true;
       }
       else if (xoStatus == DRAW ) {
            cout << "Game is a draw. \n";
            return true;
       }
       else //Continue
            return false;
    }
    CPP File

    Code:
    //tictactoe.cpp
    // To get tictactoe program running
    # include "tictactoe.h"
    int main ()
    {
      TicTacToe game;
      game.validMove(int r, int c);
      game.gameStatus();
      game.printBoard();
      game.makeMove();
      game.xoMove(int symbol);
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    constructors dont have return types

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    return type specification for constructor invalid
    See prev. posting.
    expected primary-expression before "int"
    When calling (member) functions you must not specify the type of the call parameters.
    Kurt

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Thanks Guys,

    Fixed the first error:

    But still don't understand the second totally, are you guys saying in the cpp file I can't use (int r, int c) and (int symbol)?

    Thanks

    Code:
    //tictactoe.cpp
    // To get tictactoe program running
    # include "tictactoe.h"
    int main ()
    {
      TicTacToe game;
      game.validMove(int r, int c);
      game.gameStatus();
      game.printBoard();
      game.makeMove();
      game.xoMove(int symbol);
    }

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'm saying you have to call functions like this
    Code:
    game.validMove(r, c);
    Kurt

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Agggh! I understand now!

    Thanks guys

    Trouble is I get get me program to compile, but it does absolutely nothing. Displays a blank screen quickly then disappears!

    Can anyone please advise as I can't see why it would not work?

    tictactoe.h

    Code:
    // TICTACTOE.H
    // A Program to run the Tic-Tac-Toe Program
    #include <iostream>
    using namespace std;
    #include <iomanip>
    using namespace std;
    class TicTacToe
    {
       private:
         enum Status {WIN, DRAW, CONTINUE };
         char board [3] [3];
       public:
         TicTacToe();
         void makeMove ();
         void printBoard ();
         bool validMove (int r, int c);
         bool xoMove (int input);
         Status gameStatus ();
         void holdscreen ();
    };
     
    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] = ' ';
    }
    
    bool TicTacToe::validMove (int r, int c)
    {
    
    // Validate Move Start for X //
    cout << "Enter coordinates for X: ";
        cin >> r;
        while(true) {
        if ((r>=3) || (r<0))
        cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
    else break;}
    // ** End Validate Move Start for X ** //
     
    // Validate Move Start for O //
    cout << "Enter coordinates for O: ";
        cin >> c;
        while(true) {
        if ((c>=3) || (c<0))
        cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
    else break;}
    // ** End Validate Move Start for O ** //
    
    }
    
    TicTacToe:: Status TicTacToe:: gameStatus(void)
    {
      int a;
      int r;
      int c;
      
      // Check for win in diagonals //
      if (board [0][0]!= '_' && board [0][0] == board [1][1] 
      && board [0][0] == board [2][2])
      return WIN;
      else if (board [0][2]!= '_' && board [0][2] == board [1][1] 
      && board [0][2] == board [2][0])
      return WIN;
      //** End Check for win in diagonals ** //
    
      // Check for win in rows
      if (board [0][0]!= '_' && board [0][0] == board [1][0] 
      && board [0][0] == board [2][0])
      return WIN;
        else if (board [0][1]!= '_' && board [0][1] == board [1][1] 
      && board [0][1] == board [2][2])
      return WIN; 
        else if (board [0][2]!= '_' && board [0][2] == board [1][2] 
      && board [0][2] == board [2][2])
      return WIN; 
      //** End Check for win in rows  **//  
      
      // Check for win in columns
      if (board [0][0]!= '_' && board [0][0] == board [0][1] 
      && board [0][0] == board [0][2])
      return WIN;
        else if (board [1][0]!= '_' && board [1][0] == board [1][1] 
      && board [1][0] == board [1][2])
      return WIN;
       else if (board [2][0]!= '_' && board [2][0] == board [2][1] 
      && board [2][0] == board [2][2])
      return WIN; 
      // ** End Check for win in columns ** //
    }  
    void TicTacToe::printBoard (void)
    {
         cout << "   0   1   2\n\n";
         
         for ( int r=0; r < 3; ++r ) {
             cout << r;
             
             for ( int c=0; c < 3; ++c ) {
                 cout << setw (3) << static_cast < char > ( board [r] [c] );
                 
                 if (c !=2 )
                    cout << " |";
             }
             
             if (r != 2)
                cout << "\n ______|______|______"
                     << "\n ______|______|______\n";
             }
         
         cout <<"\n\n";
    }    
    void  TicTacToe::makeMove(void)
    {
       printBoard();
       
       while ( true ) {
          if (xoMove ( 'X' ) )
             break;
       else if (xoMove ( 'O' ) )
             break;
       }
    }
    
    bool TicTacToe::xoMove (int symbol)
    {
       int x,y;
       
       do {
           cout << "Player" << static_cast < char > (symbol) << " enter move: ";
           cin >> x >> y;
           cout << '\n';
       } while ( !validMove ( x, y ) );
       
       board [x] [y] = symbol;
       printBoard();
       Status xoStatus = gameStatus();
       
       if ( xoStatus == WIN ) {
          cout << "Player " << static_cast < char > (symbol ) << " wins!\n";
          return true;
       }
       else if (xoStatus == DRAW ) {
            cout << "Game is a draw. \n";
            return true;
       }
       else //Continue
            return false;
    }
    cpp file

    Code:
    //tictactoe.cpp
    // To get tictactoe program running
    # include "tictactoe.h"
    #include <cstdlib>
    int main ()
    {
      int r; 
      int c;
      int symbol;
      TicTacToe game;
      game.validMove(r,c);
      game.gameStatus();
      game.printBoard();
      game.makeMove();
      game.xoMove(symbol);
      system ("pause");
    }

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try running your program from the commandline. That might give you a hint of what is happening.
    Kurt

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Also, you should move all your method definitions from the .h file into the .cpp file.

    Then there's something wrong with your main:
    Code:
    int main ()
    {
      // Declare some variables. Since they're not initialized, their values are pretty much random.
      int r; 
      int c;
      int symbol;
      // Create the game board. The constructor takes care of initialization.
      TicTacToe game;
      // Now this is weird. From the name validMove I would guess it takes a row and a column
      // index and tests whether placing a stone there is valid. It returns true if it is, and false if
      // it isn't.
      // That's not what validMove does, though. What it does is ignoring the row and column
      // passed and asking the user for coordinates. The peculiar thing is that it asks for
      // "the coordinates for O" and those "for X" - when what it reads is actually the row and
      // column for a single, unspecified move.
      // It then fails to return a value. Didn't the compiler complain about that?
      // You seem to be under the wrong impression that upon leaving the function, r and c
      // here have the values they got in the function. They don't.
      game.validMove(r,c);
      // This is where you check the game status. The problem here is that no moves have been
      // made yet, so the check is useless. The other problem is that you're ignoring the return
      // value. It's like asking how the game is standing, and then not listening to the answer.
      game.gameStatus();
      // Here you print the map. Looks fine, except for the superfluous cast to char.
      game.printBoard();
      // Ah, now things get interesting. Here you call a function that contains the game loop.
      game.makeMove();
      // Nope, not good. This is one more useless call.
      game.xoMove(symbol);
      system ("pause");
    }
    Basically, you seem to have misunderstood how functions work, or perhaps what main() is for. Change your main to this:
    Code:
    int main()
    {
      TicTacToe game;
      game.makeMove();
    }
    Then rewrite validMove to do what it is supposed to do (take the coordinates passed and check whether they're valid and the cell is empty, returning true if that is so and false if not. Hint: neither cin nor cout appear in this function.

    Then it should approximately work.
    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

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Thanks,

    Bit further on now:

    Trouble I have no errors and despite changing the code ValidMove function which I think was a problem it just loads a blank screen and then jumps out:

    tictactoe

    Code:
    // TICTACTOE.H
    // A Program to run the Tic-Tac-Toe Program
    #include <iostream>
    using namespace std;
    #include <iomanip>
    class TicTacToe
    {
       private:
         enum Status {WIN, DRAW, CONTINUE };
         char board [3] [3];
       public:
         TicTacToe();
         void makeMove ();
         void printBoard ();
         bool validMove (int r, int c);
         bool xoMove (int input);
         Status gameStatus ();
         void holdscreen ();
    };
     
    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] = ' ';
    }
    
    bool TicTacToe::validMove(int r, int c)
    {
    	if(board[r][c]==0)
    		return true;
    	else
    		return false;
    }
    
    TicTacToe:: Status TicTacToe:: gameStatus(void)
    {
      // Check for win in diagonals //
      if (board [0][0]!= '_' && board [0][0] == board [1][1] 
      && board [0][0] == board [2][2])
      return WIN;
      else if (board [0][2]!= '_' && board [0][2] == board [1][1] 
      && board [0][2] == board [2][0])
      return WIN;
      //** End Check for win in diagonals ** //
    
      // Check for win in rows
      if (board [0][0]!= '_' && board [0][0] == board [1][0] 
      && board [0][0] == board [2][0])
      return WIN;
        else if (board [0][1]!= '_' && board [0][1] == board [1][1] 
      && board [0][1] == board [2][2])
      return WIN; 
        else if (board [0][2]!= '_' && board [0][2] == board [1][2] 
      && board [0][2] == board [2][2])
      return WIN; 
      //** End Check for win in rows  **//  
      
      // Check for win in columns
      if (board [0][0]!= '_' && board [0][0] == board [0][1] 
      && board [0][0] == board [0][2])
      return WIN;
        else if (board [1][0]!= '_' && board [1][0] == board [1][1] 
      && board [1][0] == board [1][2])
      return WIN;
       else if (board [2][0]!= '_' && board [2][0] == board [2][1] 
      && board [2][0] == board [2][2])
      return WIN; 
      // ** End Check for win in columns ** //
    }  
    void TicTacToe::printBoard (void)
    {
         cout << "   0   1   2\n\n";
         
         for ( int r=0; r < 3; ++r ) {
             cout << r;
             
             for ( int c=0; c < 3; ++c ) {
                 cout << setw (3) << static_cast < char > ( board [r] [c] );
                 
                 if (c !=2 )
                    cout << " |";
             }
             
             if (r != 2)
                cout << "\n ______|______|______"
                     << "\n ______|______|______\n";
             }
         
         cout <<"\n\n";
    }    
    void  TicTacToe::makeMove(void)
    {
       printBoard();
       
       while ( true ) {
          if (xoMove ( 'X' ) )
             break;
       else if (xoMove ( 'O' ) )
             break;
       }
    }
    
    bool TicTacToe::xoMove (int symbol)
    {
       int x,y;
       
       do {
           cout << "Player" << static_cast < char > (symbol) << " enter move: ";
           cin >> x >> y;
           cout << '\n';
       } while ( !validMove ( x, y ) );
       
       board [x] [y] = symbol;
       printBoard();
       Status xoStatus = gameStatus();
       
       if ( xoStatus == WIN ) {
          cout << "Player " << static_cast < char > (symbol ) << " wins!\n";
          return true;
       }
       else if (xoStatus == DRAW ) {
            cout << "Game is a draw. \n";
            return true;
       }
       else //Continue
            return false;
    }
    cpp

    Code:
    //tictactoe.cpp
    // To get tictactoe program running
    # include "tictactoe.h"
      
    int main()
    {
      TicTacToe game;
      game.makeMove();
    }
    Confused!! Anyone give me any pointers?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What do you mean "jump out"? Simply ends? Crashes? Any error messages?
    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

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    By jumping-out I mean the program compiles, but just shows a black dos box and then it just disappears after about a second. No errors though?

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Then do what zuk said. Start a command line (Start->Run "cmd") and navigate to the directory the compiled exe is in. Then just type in the name of the exe.
    This will make the console stay open and you can see if there's any error message.
    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

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Ta Guys,

    Tried running it in dos, but all it does it wait about a second and then just jumps back to the original command prompt...

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Put

    cout << flush;

    at the end of printBoard and see if the output changes.
    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

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Cheers for the idea corned bee, but still just jumps out. My complier hates me! :-(

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21