[英]Vector attributes on class instance in returned pointer attribute disappearing
I'm implementing a kind of tree search that requires being able to get a "most promising node" from a tree and then doing something with that node in order to update the remainder of the tree for the next iteration.我正在实现一种树搜索,它需要能够从树中获取“最有希望的节点”,然后对该节点执行某些操作,以便为下一次迭代更新树的其余部分。
Problem : an object pointer Board*
has vector attributes that seem to change between the return
of the function producing them, and the Board*
value holding them in the calling environment.问题:object 指针
Board*
的向量属性似乎在 function 的return
值和在调用环境中保存它们的Board*
值之间发生变化。
My output:我的 output:
>>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)
What I'd expect:我的期望:
>>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;
}
Not sure if this is the only problem, but here不确定这是否是唯一的问题,但在这里
for(int _=0;_<2;_++){
Board new_state = board -> getcopy();
children.push_back(new Node(this,&new_state,depth+1));
}
Your create a copy of Board
which only lives inside the for
loop.您创建了仅存在于
for
循环内的Board
副本。 It gets destroyed automatically at }
.它在
}
处自动销毁。 Hence the pointers you store in the node are dangling.因此,您存储在节点中的指针是悬空的。 The point to objects that are long gone.
指向早已不复存在的物体。
Note that your getcopy
is a bit weird.请注意,您的
getcopy
。 You should use a copy constructor instead.您应该改用复制构造函数。 In general, when your type mangages resources via raw pointers (not sure if it actually is the case) then you need to respect the rule of 3/5.
通常,当您的类型通过原始指针管理资源时(不确定是否确实如此),那么您需要遵守 3/5 规则。 In any case you can copy a board simply by writing
Board new_state = *board;
在任何情况下,您都可以通过编写
Board new_state = *board;
来复制一个板子。 (assuming the compiler generated copy constructor does the right thing). (假设编译器生成的复制构造函数做正确的事)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.