返回的指针属性中类实例上的矢量属性消失
Vector attributes on class instance in returned pointer attribute disappearing
我正在实现一种树搜索,它需要能够从树中获取"最有前途的节点",然后对该节点执行某些操作,以便为下一次迭代更新树的其余部分。
问题:对象指针Board*
具有矢量属性,这些属性似乎在生成它们的函数的return
与调用环境中保存它们的Board*
值之间发生变化。
我的输出:
>>g++ -std=c++17 -o tmp.out tests/test.cpp // <- require c++17 for other parts of the project
>>./tmp.out
Best leaf by tree traversal has score: 8
Best leaf associated state has -1977735524 values in its attribute vector though! (Should be 4)
我所期望的:
>>g++ -std=c++17 -o tmp.out tests/test.cpp // <- require c++17 for other parts of the project
>>./tmp.out
Best leaf by tree traversal has score: 8
Best leaf associated state has 4 values in its attribute vector though! (Should be 4)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class Board{
vector<int> attribute;
string name;
public:
Board(){
attribute = {1,2,3,4};
name = "nonempty name";
}
Board getcopy(){
return *this;
}
int AttrLen(){
return attribute.size();
}
};
class Node{
Board* board;
Node* parent;
std::vector<Node*> children;
int depth=0;
int score=0;
bool terminal=false;
public:
Node(Node* _parent, Board* _board,int _depth){
parent = _parent;
board = _board;
depth = _depth;
// randomize score
score = rand() % 10;
if(depth<2){
for(int _=0;_<2;_++){
Board new_state = board -> getcopy();
children.push_back(new Node(this,&new_state,depth+1));
}
} else {
children = {};
terminal=true;
}
}
int getScore(){
return score;
}
bool isTerminal(){
return terminal;
}
Node* getBestChild(){
if(!terminal){
if(children[0] ->getScore() > children[1] -> getScore()){
return children[0];
} else {
return children[1];
}
} else {
return nullptr;
}
}
Board* getStateptr(){
return board;
}
};
int main(){
// make a board
Board my_board = Board();
// make a root node
Node root = Node(nullptr, &my_board, 0);
Node* best_child = root.getBestChild();
while(!best_child -> isTerminal()){
best_child = best_child -> getBestChild();
}
cout << "Best leaf by tree traversal has score: " << best_child -> getScore() << endl;
cout << "Best leaf associated state has " << best_child -> getStateptr() ->AttrLen() << " values in its attribute vector though! (Should be 4)" << endl;
}
不确定这是否是唯一的问题,但在这里
for(int _=0;_<2;_++){
Board new_state = board -> getcopy();
children.push_back(new Node(this,&new_state,depth+1));
}
您创建一个仅存在于for
循环内的Board
副本。它在}
时自动销毁。因此,您存储在节点中的指针是悬空的。指向早已消失的对象。
请注意,您的getcopy
有点奇怪。应改用复制构造函数。通常,当您的类型通过原始指针管理资源时(不确定是否确实如此(,则需要遵守 3/5 规则。在任何情况下,您都可以简单地通过编写Board new_state = *board;
来复制电路板(假设编译器生成的复制构造函数做了正确的事情(。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- 将地址分配给本地指针后,公共对象的变量将消失
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- 返回的指针属性中类实例上的矢量属性消失
- 继承和派生属性在C++中消失
- 创建一个新对象并将该对象存储在新类的向量中,属性消失