我正在用一个简单的AI编写一个简单的tictactoe游戏。我在初始化游戏对象时遇到错误。我的游戏对象使用名为setPlayerNum的方法初始化了两个玩家对象,每个对象的玩家编号属性都设置为“一个”或“两个”。尽管我从不使用“ this”关键字,但是却收到一条错误消息,指出“ this is nullptr”以及写访问冲突。
我尝试过取消引用播放器对象,以及将播放器编号属性设置为公共变量(以防出现范围错误)。都不适合我。
int main() {
game g;
g.printTitleCard();
std::system("pause");
return 0;
}
#include "board.h"
#include "player.h"
class game {
public:
game();
~game();
void printTitleCard();
void play();
int victory();
void victoryScreen(int winner);
private:
board * gameBoard;
player * player_one;
player * player_two;
int turns;
int maxTurns = 9;
};
game::game() {
gameBoard = new board();
player_one->setPlayerNum(1);
player_two->setPlayerNum(2);
}
class player {
public:
player();
~player();
void setPlayerNum(int);
int getPlayerNum();
void updateScore(int);
int retrieveWins();
int retrieveLoses();
int retrieveTies();
private:
int playerNum = 0;
int wins = 0;
int loses = 0;
int ties = 0;
};
void player::setPlayerNum(int num) {
playerNum = num;
}
答案 0 :(得分:3)
game::game() {
gameBoard = new board();
player_one->setPlayerNum(1);
player_two->setPlayerNum(2);
}
不会在调用player
之前实例化任何setPlayerNum
,这意味着没有player
的有效实例可以调用setPlayerNum
。之后,程序进入undefined behaviour,所有投注都关闭了。
解决方案:
解决此问题的最佳方法是回到
class game {
public:
game();
~game();
void printTitleCard();
void play();
int victory();
void victoryScreen(int winner);
private:
board * gameBoard;
player * player_one;
player * player_two;
int turns;
int maxTurns = 9;
};
并且不要对gameBoard
,player_one
和player_two
使用指针。
board * gameBoard;
player * player_one;
player * player_two;
成为
board gameBoard;
player player_one;
player player_two;
和
game::game() {
gameBoard = new board();
player_one->setPlayerNum(1);
player_two->setPlayerNum(2);
}
成为
game::game() {
player_one.setPlayerNum(1);
player_two.setPlayerNum(2);
}
仅在绝对必要时使用new
,在几乎没有库容器和智能指针的世界中。这里的更多信息:Why should C++ programmers minimize use of 'new'?
不相关:
如果更改播放器构造函数以将播放器编号作为参数,则setPlayerNum
可能变得不必要,而game
的构造函数可能会变成
game::game(): player_one(1), player_two(2){
}
有什么办法可以将它们保留为指针?
是的,但这不是一个好主意,因为您承担了很多额外的责任。在使用球员之前,您天真地new
个球员。您必须delete
个播放器和game
中的析构器,并且您必须编写自己的副本(如果需要,请移动)构造函数和赋值(并移动赋值)运算符,以确保对象已正确复制和分配(有关为何需要此材料的更多信息,请参见What is The Rule of Three?)。
game::game() {
gameBoard = new board();
player_one = new player; //added to get player instance
player_two= new player; // added to get player instance
player_one->setPlayerNum(1);
player_two->setPlayerNum(2);
}
// new function: copy constructor
game::game(const game & source) {
gameBoard = new board(*source.gameBoard);
player_one = new player(*source.player_one);
player_two= new player(*source.player_two);
turns = source.turns;
maxTurns = source.maxTurns;
}
game::~game() {
delete gameBoard; // you needed to have this if you didn't already
delete player_one; // added to release player instance
delete player_two; //added to release player instance
}
// new function swap function (used by assignment operator)
game::swap(game & source) {
std::swap(gameBoard, source.gameBoard);
std::swap(player_one , source.player_one);
std::swap(player_two, source.player_two);
std::swap(turns, source.turns);
std::swap(maxTurns, source.maxTurns);
}
// new function assignment operator
game & operator=(game source)
{
swap(source);
return * this;
}
查看您必须编写的所有其他内容。可能其中有一个或两个错误。
赋值运算符正在利用Copy and Swap Idiom。它通常不是最快的分配方式,但它很容易编写且很难出错。我从复制和交换开始,一直使用它,直到生活证明它是性能瓶颈。
如果gameBoard
,player_one
和player_two
是自动变量,除非board
写得不好,否则编译器将为您生成析构函数和所有复制代码。您的目标之一应该是使own a resource的所有类都遵守Rule of Three or Five,以便包含它们的类可以利用Rule of Zero。