inheritance - C++ Error in compiling due to pointer class -
my code doesn't compile when try running it. many errors don't understand why errors pop in first place. of errors involve header file. of errors i'm missing ';' before '*' in header file. furthermore says int assumed in header file comment in code happens.
this error im getting compiler:
1`>\\csupdatevm\labusers\uuu_uuuuu\documents\visual studio 2013\projects\expection\expection\game_implementation.cpp(25): error c2276: '&' : illegal operation on bound member function expression`
in header file have:
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; //#include <exception> class board{ public: void displayboard(); void createboard(); void insert(int temp_move, char temp_player); bool islegal(int move); vector<char> getboard(); private: const int num_squares = 9; vector<char> board; }; class abstractplayer{ public: int virtual move(char temp_player) = 0; char virtual selectpiece()=0; char askyesno(string question); int asknumber(string question, int high, int low = 0); char winner(vector<char> const & another__temp_board); protected: char go_first; bool first_turn = true; char human; char computer; const char x = 'x'; const char o = 'o'; const char empty = ' '; const char tie = 't'; const char no_one = 'n'; board another_board; }; class human:public abstractplayer{ public: int move(char temp_player); char selectpiece(); }; class computer:public abstractplayer{ public: int move(char temp_player); char selectpiece(); }; class game{ public: void play(); void instructions(); void announcewinner(char winner, char computer, char human); private: human* human;//error computer* cpu;//error board board; char turn = 'x'; int move; };
in human cpp file have:
#include "tictactoe.h" char human::selectpiece() { if (first_turn == true) { go_first = askyesno("do require first move?"); if (go_first == 'y') { cout << "\nthen take first move. need it.\n"; return 'x'; } else { cout << "\nyour bravery undoing... go first.\n"; return 'o'; } first_turn = false; } else return 'x'; } int human::move(char temp_player) { int move = asknumber("where move?", 9); while (!another_board.islegal(move)) { cout << "\nthat square occupied, foolish human.\n"; move = asknumber("where move?", 9); } cout << "fine...\n"; return move; }
in computer cpp file have:
#include "tictactoe.h" char computer::selectpiece() { return 'o'; } int computer::move(char temp_player) { cout << "i shall take square number "; vector<char> temp_board_yo=another_board.getboard(); // if computer can win on next move, make move (int move = 0; move < 9; ++move) { if (another_board.islegal(move)) { temp_board_yo[move] = computer; if (winner(temp_board_yo) == computer) { cout << move << endl; return move; } // done checking move, undo temp_board_yo[move] = empty; } } // if human can win on next move, block move char human = 'x'; (int move = 0; move < 9; ++move) { if (another_board.islegal(move)) { temp_board_yo[move] = human; if (winner(temp_board_yo) == human) { cout << move << endl; return move; } // done checking move, undo temp_board_yo[move] = empty; } } // best moves make, in order const int best_moves[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 }; // since no 1 can win on next move, pick best open square (int = 0; < 9; ++i) { int move = best_moves[i]; if (another_board.islegal(move)) { cout << move << endl; return move; } } }
in board cpp file have
#include "tictactoe.h" void board::createboard() { (int = 0; < num_squares; i++) board.push_back(' '); } void board::displayboard() { cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2]; cout << "\n\t" << "---------"; cout << "\n\t" << board[3] << " | " << board[4] << " | " << board[5]; cout << "\n\t" << "---------"; cout << "\n\t" << board[6] << " | " << board[7] << " | " << board[8]; cout << "\n\n"; } void board::insert(int temp_move,char temp_player) { board[temp_move] = temp_player; } bool board::islegal(int move) { return (board[move] ==' '); } vector <char> board::getboard() { return board; }
in abstractplayer cpp file have:
#include "tictactoe.h" char abstractplayer::askyesno(string question) { char response; { cout << question << " (y/n): "; cin >> response; } while (response != 'y' && response != 'n'); return response; } int abstractplayer::asknumber(string question, int high, int low) { int number; { cout << question << " (" << low << " - " << high << "): "; cin >> number; } while (number > high || number < low); return number; } char abstractplayer::winner(vector<char> const & another_temp_board) { // possible winning rows const int winning_rows[8][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 } }; const int total_rows = 8; // if winning row has 3 values same (and not empty), // have winner (int row = 0; row < total_rows; ++row) { if ((another_temp_board[winning_rows[row][0]] != empty) && (another_temp_board[winning_rows[row][0]] == another_temp_board[winning_rows[row][1]]) && (another_temp_board[winning_rows[row][1]] == another_temp_board[winning_rows[row][2]])) { return another_temp_board[winning_rows[row][0]]; } } // since nobody has won, check tie (no empty squares left) if (count(another_temp_board.begin(), another_temp_board.end(), empty) == 0) return tie; // since nobody has won , isn't tie, game ain't on return no_one; }
in game_implementation cpp have
#include "tictactoe.h" void game::instructions() { cout << "welcome ultimate man-machine showdown: tic-tac-toe.\n"; cout << "--where human brain pit against silicon processor\n\n"; cout << "make move known entering number, 0 - 8. number\n"; cout << "corresponds desired board position, illustrated:\n\n"; cout << " 0 | 1 | 2\n"; cout << " ---------\n"; cout << " 3 | 4 | 5\n"; cout << " ---------\n"; cout << " 6 | 7 | 8\n\n"; cout << "prepare yourself, human. battle begin.\n\n"; } void game::play() { instructions(); board.createboard(); while (human->winner(board.getboard()) =='n') { if (turn == human->selectpiece())//human goes { //move = humanmove(board, human); move = human->move(human->selectpiece()); board.insert(move, human->selectpiece()); turn = cpu->selectpiece(); } else//computer goes { //move = computermove(board, computer); move = cpu->move(cpu->selectpiece()); board.insert(move, cpu->selectpiece()); turn = human->selectpiece(); } board.displayboard(); } announcewinner(cpu->winner(board.getboard()), cpu->selectpiece(), human->selectpiece());//error: says not take 1 argument } void game::announcewinner(char winner, char computer, char human) { if (winner == computer) { cout << winner << "'s won!\n"; cout << "as predicted, human, triumphant once more -- proof\n"; cout << "that computers superior humans in regards.\n"; } else if (winner == human) { cout << winner << "'s won!\n"; cout << "no, no! cannot be! somehow tricked me, human.\n"; cout << "but never again! i, computer, swear it!\n"; } else { cout << "it's tie.\n"; cout << "you lucky, human, , somehow managed tie me.\n"; cout << "celebrate... best ever achieve.\n"; } }
and in test file have
#include "tictactoe.h" int main() { game g; g.play(); }
the compiler not know human
, computer
, , board
when processes lines:
human* human;//error computer* cpu;//error board board;
you need add forward declarations of human
, computer
before definition of game
since declaring pointers types in game
.
you need move definition of board
before definition of game
since declaring object of type in game
.
p.s. there may other errors need fixed too.
update
while (human->winner(& board.getboard) =='n')
is not right since board.getboard
member function. need there is:
while (human->winner(board.getboard()) =='n') // ^^ missing parens
however, work, you'll need change interface of abstractplayer::winner
char winner(vector<char> another_temp_board)
or
char winner(vector<char> const& another_temp_board)
Comments
Post a Comment