Problem with C++ Program
Jun 3, 2013 at 5:09pm UTC
Hi, I'm writing a C++ program for my class and I'm relatively new to this language. I'm using eclipse c/c++ to write my code and I've come up with the following:
TictactoeGame.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
// Member-function definitions for class Game that plays Tic Tac Toe game
#include <iostream>
#include "TictactoeGame.h"
using namespace std;
// constructor to initialize member data location and BoardArray
TictactoeGame::TictactoeGame(string selection)
{
Location = selection; //
for ( int i = 0; i < 3; i++ ) // display a row
{
for ( int j = 0; j < 3; j++ )
BoardArray[i][j] = ' ' ; // initialize a cell
}
}
// function to set player's selection of location
// ensures that the location is a valid location in the 3 x3 grid board
void TictactoeGame::setLocation( string location)
{
int index = 0;
if ( location.compare("1 1" ) == 0) // row 1 column 1
index = 1;
else if ( location.compare("1 2" ) == 0) // row 1 column 2
index = 2;
else if ( location.compare("1 3" ) == 0) // row 1 column 3
index = 3;
else if ( location.compare("2 1" ) == 0) // row 2 column 1
index = 4;
else if ( location.compare("2 2" ) == 0) // row 2 column 2
index = 5;
else if ( location.compare("2 3" ) == 0) // row 2 column 3
index = 6;
else if ( location.compare("3 1" ) == 0) // row 3 column 1
index = 7;
else if ( location.compare("3 2" ) == 0) // row 3 column 2
index = 8;
else if ( location.compare("3 3" ) == 0) // row 3 column 3
index = 9;
else
index = -1;
switch (index)
{
case 1 :
if ((BoardArray[0][0] == 'X' ) || (BoardArray[0][0] == 'O' ))
index = -2;
else
BoardArray[0][0] = 'X' ;
break ;
case 2 :
if ((BoardArray[0][1] == 'X' ) || (BoardArray[0][1] == 'O' ))
index = -2;
else
BoardArray[0][1] = 'X' ;
break ;
case 3 :
if ((BoardArray[0][2] == 'X' ) || (BoardArray[0][2] == 'O' ))
index = -2;
else
BoardArray[0][2] = 'X' ;
break ;
case 4 :
if ((BoardArray[1][0] == 'X' ) || (BoardArray[1][0] == 'O' ))
index = -2;
else
BoardArray[1][0] = 'X' ;
break ;
case 5 :
if ((BoardArray[1][1] == 'X' ) || (BoardArray[1][1] == 'O' ))
index = -2;
else
BoardArray[1][1] = 'X' ;
break ;
case 6 :
if ((BoardArray[1][2] == 'X' ) || (BoardArray[1][2] == 'O' ))
index = -2;
else
BoardArray[1][2] = 'X' ;
break ;
case 7 :
if ((BoardArray[2][0] == 'X' ) || (BoardArray[2][0] == 'O' ))
index = -2;
else
BoardArray[2][0] = 'X' ;
break ;
case 8 :
if ((BoardArray[2][1] == 'X' ) || (BoardArray[2][1] == 'O' ))
index = -2;
else
BoardArray[2][1] = 'X' ;
break ;
case 9 :
if ((BoardArray[2][2] == 'X' ) || (BoardArray[2][2] == 'O' ))
index = -2;
else
BoardArray[2][2] = 'X' ;
break ;
case -1 :
cout << "Invalid input, try again: " << endl;
break ;
default :
cout << "Invalid cell" << endl;
} //end case
if (index == -2)
cout << "The selected cell already filled , please choose another cell: " << endl;
} // end function setLocation
// function to retrieve the player's selected location
std::string TictactoeGame::getLocation() const
{
return Location;
} // end function setLocation
// prompt an input message
void TictactoeGame::displayMessage() const
{
cout << "\nWelcome to play Tic Tac Toe \n" << endl;
cout << "Enter a cell location (e.g. 1 3 represents first row, third column) or -1 to quit: " ;
cin >> Location;
} // end function displayMessage
// display the 3 x 3 grid board status at the completion of each move by both the player and the game
void TictactoeGame::drawBoard() const
{
for ( int i = 0; i < 3; i++ ) // display a row
for ( int j = 0; j < 3; j++ )
{
cout << "| " << BoardArray[i][j] << " " ; // draw a column
cout << endl << "-------------" ;
}
} // end function displayMessage
// determine if all squares are filled and who win
void determingResult() const
{
cout << "This function hasn't down " ;
}
and
TictactoeGame.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
* TictactoeGame.h
*
* Created on: Jun 3, 2013
* Author: Blue Fire
*/
// Define of class Game that plays Tic Tac Toe with a user
// Member functions are defined in Game.cpp
#include <string> // program uses C++ standard string class
// TictactoeGame class definition
class TictactoeGame
{
public :
explicit TictactoeGame(std::string); // constructor initialize location and grid board
void setLocation( std::string ); // set selection of location
std::string getLocation() const ; //retrieve the selection of location
void displayMessage() const ; // prompt an input message
void drawBoard() const ; // display the 3 x 3 grid board
void determingResult() const ; // determine if all squares are filled and who win
private :
std::string Location; //player's selection of location
char BoardArray[3][3];
}; // end of class Game
The problem is that when compiling I get 2 errors and I'm not sure how to fix them. They are both in TictactoeGame.cpp:
1. line 125 with an error message of
no match for 'operator>>' in 'std::cin >> ((const TictactoeGame*)this)->TictactoeGame::Location'
2. line 143 with an error message of
non-member function `void determingResult()' cannot have `const' method qualifier
Any help for solving these two errors and getting the program to compile would be appreciated. Thanks ahead of time
Jun 3, 2013 at 5:13pm UTC
Let begin from the error message
non-member function `void determingResult()' cannot have `const' method qualifier
What is not clear in this text?
Jun 3, 2013 at 5:14pm UTC
1. line 125 with an error message of no match for 'operator>>' in 'std::cin >> ((const TictactoeGame*)this)->TictactoeGame::Location'
You've defined
TictactoeGame::displayMessage() to be a const method, but you're trying to modify one of its data members within the method.
2. line 143 with an error message of non-member function `void determingResult()' cannot have `const' method qualifier
You've defined
determingResult to be a global function, not a method of
TictactoeGame .
Last edited on Jun 3, 2013 at 5:15pm UTC
Topic archived. No new replies allowed.