gpt4 book ai didi

c++ - 简单的 C++ 指针 : How to link a child node to parent node

转载 作者:行者123 更新时间:2023-11-28 02:20:27 24 4
gpt4 key购买 nike

我使用一个非常简单的节点结构来实现迭代深化 DFS。但是,我后来在将子节点链接到父节点时遇到了问题。

struct Node
{
std::vector<int> config;
int depth;
int action; //0 up 1 down 2 left 3 right
Node * parent;
bool operator<(const Node& rhs) const
{
return depth < rhs.depth;
}
};

稍后在我的代码中,当我尝试做这样的事情时,我遇到了严重的错误:

int main()
{
cout << "Welcome to IDDFS 8-puzzle solver. Now calculating movements... \n";

//Initialize base variables
struct Node initial = {orig_config, 0, 0}; //config, depth, action, parent.
struct Node goal_node;
priority_queue<Node> frontier;
std::vector<Node> visited;
frontier.push(initial);
int Current_Max_Depth = 1;
while(frontier.size()>0)
{
struct Node Next = frontier.top();
frontier.pop();
visited.push_back(Next);
if(Next.depth < Current_Max_Depth)
{
int pos_of_hole = Find_Position_of_Hole(Next.config);
if(pos_of_hole==0)
{
std::vector<int> Down_Child = Move_Down(Next.config);
struct Node Down_Node = {Down_Child,Next.depth+1,1,&Next};
if(!(std::find(visited.begin(), visited.end(), Down_Child)!=visited.end()))
{
if(Goal_Test(Down_Child))
{
goal_node = Down_Node;
break;
}
frontier.push(Down_Node);
}

std::vector<int> Right_Child = Move_Right(Next.config);
struct Node Right_Node = {Right_Child,Next.depth+1,3,&Next};
if(!(std::find(visited.begin(), visited.end(), Right_Child)!=visited.end()))
{
if(Goal_Test(Right_Child))
{
goal_node = Right_Node;
break;
}
frontier.push(Right_Node);
}
}
}
}

我想要做的就是将这个子节点(称为 Down_Node)链接到它的父节点(称为 Next)。但是,如果 Next 本身不是 Node*,我该怎么办?

问题出在指向 Next 的指针上。我尝试了 &(Next)、*Next 等,但无法正常工作。我尝试制作一个指向 Next 的节点指针变量,但我还是无法让它工作。我正在尝试解决这个问题,但遇到了很多麻烦。导致我垮台的是我对 C++ 指针的误解。

编辑:当我尝试使用 &Next 传递引用时,我收到一个我无法理解的巨大乱码错误。

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/algorithm:62, from iddfs.cpp:8:

/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator > >, _Tp = std::vector >]’: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:4224: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator > >, _Tp = std::vector

]’ iddfs.cpp:225: instantiated from here /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:174: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Node*, _Container = std::vector > == __val’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:4224: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator > >, _Tp = std::vector ]’ iddfs.cpp:225: instantiated from here /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:178: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Node*, _Container = std::vector > == __val’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:182: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Node*, _Container = std::vector > == __val’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:186: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Node*, _Container = std::vector > == __val’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:194: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Node*, _Container = std::vector > == __val’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:198: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Node*, _Container = std::vector > == __val’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:202: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Node*, _Container = std::vector > == __val’

有谁知道如何开始分析这个错误?我正在其中查找行号或其他信息,然后从那里开始。

Edit2:最终变成了完全不同的东西。为我的节点类型实现 == 时 C++ 中的模板错误。我最终将访问的变量更改为整数 vector 的 vector 。这是一种 hack 和解决方法,但它会满足我的需要。

最佳答案

C++ 中的模板错误是所有编程中最令人讨厌的错误之一。话虽如此,您肯定需要通过引用传递 Next,但模板错误来自其他地方。在这种情况下,您似乎需要为您的 Node 类型实现 operator==。根据错误消息破译的唯一快速方法是查看它正在提示 no match for 'operator==' in ...,不幸的是,对于我们这些凡人来说,剩下的错误消息并不是真正有用,因为错误实际上是在 C++ 标准库的内部引发的,它实际上尝试在 Node 类型上使用 operator==,但是一个只有非常勇敢的人才能去的地方。

关于c++ - 简单的 C++ 指针 : How to link a child node to parent node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32708659/

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