C++ 标准::错误的alloc whit标准::向量,Tic-Tac-Toe
我正在尝试实现我自己的Tic-Tac-Toe游戏,并制作一棵树(数据结构),其中包含所有可能的移动(对于最小-最大算法) 在函数C++ 标准::错误的alloc whit标准::向量,Tic-Tac-Toe,c++,stdvector,tic-tac-toe,bad-alloc,minmax,C++,Stdvector,Tic Tac Toe,Bad Alloc,Minmax,我正在尝试实现我自己的Tic-Tac-Toe游戏,并制作一棵树(数据结构),其中包含所有可能的移动(对于最小-最大算法) 在函数Add_Match()中的某一点上,我得到了以下运行时错误: terminate在抛出'std::bad_alloc'实例后调用 什么():std::坏的` main()(intTic Tac Toe.cpp)的有趣部分: if(胜利(董事会)){ //运动把胜利交给了玩家1,我们拯救了它 期末考试,设1分; 比赛。推回(决赛); //复赛重设 板。重置(); fin
Add_Match()
中的某一点上,我得到了以下运行时错误:
terminate在抛出'std::bad_alloc'实例后调用
什么():std::坏的`
main()
(intTic Tac Toe.cpp
)的有趣部分:
if(胜利(董事会)){
//运动把胜利交给了玩家1,我们拯救了它
期末考试,设1分;
比赛。推回(决赛);
//复赛重设
板。重置();
final.Reset();
内存。添加_匹配(匹配);
match.clear();
匹配。推回(选项());
matchs++;
}
我的Board
课程:
课程板{
私人:
int n=3;
std::向量板;
公众:
董事会(){
线路板=标准::向量(n,标准::向量(n));
重置();
}
无效重置(){
对于(大小i=0;i std::cout这里有一个明显的错误:
else
{
std::vector<Choice> childs = ptr->get_Childs();
for (size_t i = 0; i < childs.size(); i++)
{
ptr=&childs[i]; // <-- Bug
//...
您返回的是向量的副本,而不是实际的childs
向量。修复方法是返回引用,而不是副本:
std::vector<Choice>& get_Childs(){return childs_;}
std::vector&get_Childs(){return Childs_;}
然后:
std::vector<Choice>& childs = ptr->get_Childs();
std::vector&childs=ptr->get_childs();
注意参考返回值
std::vector<Choice>& childs = ptr->get_Childs();