我尝试运行时,代码无法编译。我收到很多错误,我不明白为什么这些错误会首先出现。一些错误涉及我的头文件。有些错误说我错过了&#39 ;;'之前' *'在我的头文件中。此外,它说在我的头文件中假设int我会在我的代码中发表评论。
这是我从编译器获得的错误:
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`
在我的头文件中,我有:
#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;
};
在我的人类cpp文件中,我有:
#include "tictactoe.h"
char Human::selectPiece()
{
if (first_turn == true)
{
go_first = askYesNo("Do you require the first move?");
if (go_first == 'y')
{
cout << "\nThen take the first move. You will need it.\n";
return 'X';
}
else
{
cout << "\nYour bravery will be your undoing... I will go first.\n";
return 'O';
}
first_turn = false;
}
else
return 'X';
}
int Human::move(char temp_player)
{
int move = askNumber("Where will you move?", 9);
while (!another_board.isLegal(move))
{
cout << "\nThat square is already occupied, foolish human.\n";
move = askNumber("Where will you move?", 9);
}
cout << "Fine...\n";
return move;
}
在我的计算机cpp文件中,我有:
#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 that move
for (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 this move, undo it
temp_board_yo[move] = EMPTY;
}
}
// if human can win on next move, block that move
char human = 'X';
for (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 this move, undo it
temp_board_yo[move] = EMPTY;
}
}
// the best moves to make, in order
const int BEST_MOVES[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 };
// since no one can win on next move, pick best open square
for (int i = 0; i < 9; ++i)
{
int move = BEST_MOVES[i];
if (another_board.isLegal(move))
{
cout << move << endl;
return move;
}
}
}
在我的电路板cpp文件中我有
#include "tictactoe.h"
void Board::createBoard()
{
for (int i = 0; i < 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;
}
在我的abstractplayer cpp文件中,我有:
#include "tictactoe.h"
char Abstractplayer::askYesNo(string question)
{
char response;
do
{
cout << question << " (y/n): ";
cin >> response;
} while (response != 'y' && response != 'n');
return response;
}
int Abstractplayer::askNumber(string question, int high, int low)
{
int number;
do
{
cout << question << " (" << low << " - " << high << "): ";
cin >> number;
} while (number > high || number < low);
return number;
}
char Abstractplayer::winner(vector<char> const & another_temp_board)
{
// all 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 any winning row has three values that are the same (and not EMPTY),
// then we have a winner
for (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 for a tie (no empty squares left)
if (count(another_temp_board.begin(), another_temp_board.end(), EMPTY) == 0)
return TIE;
// since nobody has won and it isn't a tie, the game ain't over
return NO_ONE;
}
在我的game_implementation cpp中我有
#include "tictactoe.h"
void Game::instructions()
{
cout << "Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n";
cout << "--where human brain is pit against silicon processor\n\n";
cout << "Make your move known by entering a number, 0 - 8. The number\n";
cout << "corresponds to the desired board position, as 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. The battle is about to 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 does not take 1 argument
}
void Game::announceWinner(char winner, char computer, char human)
{
if (winner == computer)
{
cout << winner << "'s won!\n";
cout << "As I predicted, human, I am triumphant once more -- proof\n";
cout << "that computers are superior to humans in all regards.\n";
}
else if (winner == human)
{
cout << winner << "'s won!\n";
cout << "No, no! It cannot be! Somehow you tricked me, human.\n";
cout << "But never again! I, the computer, so swear it!\n";
}
else
{
cout << "It's a tie.\n";
cout << "You were most lucky, human, and somehow managed to tie me.\n";
cout << "Celebrate... for this is the best you will ever achieve.\n";
}
}
在我的测试文件中我有
#include "tictactoe.h"
int main()
{
Game g;
g.play();
}
答案 0 :(得分:1)
编译器在处理行时对Human
,Computer
和Board
一无所知:
Human* Human;//error
Computer* cpu;//error
Board board;
您需要在Human
的定义之前添加Computer
和Game
的前向声明,因为您只是在Game
中声明指向这些类型的指针。
您需要在Board
的定义之前移动Game
的定义,因为您在Game
声明了该类型的对象。
P.S。可能还有其他错误需要修复。
<强>更新强>
while (Human->winner(& board.getBoard) =='N')
不对,因为board.getBoard
是成员函数。你需要的是:
while (Human->winner(board.getBoard()) =='N')
// ^^ Missing parens
但是,要实现此功能,您需要将Abstractplayer::winner
的界面更改为
char winner(vector<char> another_temp_board)
或
char winner(vector<char> const& another_temp_board)