gpt4 book ai didi

c++ - 没有调用 'this was nullptr"时如何修复 "this"

转载 作者:行者123 更新时间:2023-11-27 23:40:11 25 4
gpt4 key购买 nike

我正在用一个简单的人工智能编写一个简单的井字游戏。初始化我的游戏对象时出现错误。我的游戏对象初始化两个玩家对象,每个玩家对象都使用名为 setPlayerNum 的方法将其玩家编号属性设置为“一”或“二”。尽管我从不使用“this”关键字,但我收到一条错误消息,指出“this was a nullptr”以及写访问冲突。

我已经尝试取消对玩家对象的引用以及将玩家编号属性设为公共(public)变量(以防出现范围错误)。两者都不适合我。

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;
}

最佳答案

game::game() {
gameBoard = new board();
player_one->setPlayerNum(1);
player_two->setPlayerNum(2);
}

在调用 setPlayerNum 之前不实例化任何 player,这意味着没有有效的 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;
};

并且不要为 gameBoardplayer_oneplayer_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){
}

附录

有什么方法可以将它们保留为指针吗?

是的,但这不是一个好主意,因为您承担了大量的额外责任。在使用它们之前,您需要在构造函数中新建 播放器。您必须在析构函数中删除玩家和游戏,并且您必须编写自己的拷贝(如果需要则移动)构造函数和赋值(并移动赋值) 运算符以确保对象被正确复制和分配(请参阅 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 .它通常不是最快的分配方式,但它非常容易编写并且很难出错。我从 Copy 和 Swap 开始,一直使用它直到生活证明它是性能瓶颈。

如果 gameBoardplayer_oneplayer_two 是自动变量,编译器将为您生成析构函数和所有复制代码,除非 board 写得不好。你的目标之一应该是让你的所有类(class)都成为 own a resource观察 Rule of Three or Five这样包含它们的类可以利用 Rule of Zero .

关于c++ - 没有调用 'this was nullptr"时如何修复 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com