我有一个非常简单的节点结构,用于实现Iterative Deepening 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的指针给出了麻烦。我试过&amp;(下一个),*接下来等等但是无法让它工作。我尝试制作一个指向Next的Node指针变量,但我再次无法使其工作。我试图解决这个问题,但遇到了很多麻烦。这是我对C ++指针的误解导致我的垮台。
编辑:当我尝试使用&amp; Next传递引用时,我收到一个巨大的错误,我不理解。
在/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/algorithm:62中包含的文件中, 来自iddfs.cpp:8:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7 /../../../../包括/ C ++ / 4.4.7 /比特/ stl_algo.h: 在函数'_RandomAccessIterator std :: __ find(_RandomAccessIterator, _RandomAccessIterator,const _Tp&amp;,std :: random_access_iterator_tag)[with _RandomAccessIterator = __gnu_cxx :: __ normal_iterator&gt; &gt;,_ Tp = std :: vector&gt;]': /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:4224: 从'_IIter std :: find(_IIter,_IIter,const _Tp&amp;)实例化[与... _IIter = __gnu_cxx :: __ normal_iterator&gt; &gt;,_ Tp = std :: vector
]'iddfs.cpp:225:从这里实例化/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7 /bits/stl_algo.h:174:错误:不匹配'operator =='in “__first .__ gnu_cxx :: __ normal_iterator&LT; _Iterator, _Container&gt; :: operator * with _Iterator = Node *,_ _Container = std :: vector&gt; == __val' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:4224: 从'_IIter std :: find(_IIter,_IIter,const _Tp&amp;)实例化[与... _IIter = __gnu_cxx :: __ normal_iterator&gt; &gt;,_ Tp = std :: vector ]'iddfs.cpp:225:从这里实例化/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ stl_algo.h:178:错误:不匹配'operator =='in “__first .__ gnu_cxx :: __ normal_iterator&LT; _Iterator, _Container&gt; :: operator * with _Iterator = Node *,_ _Container = std :: vector&gt; == __val' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:182:错误:不匹配'operator =='in “__first .__ gnu_cxx :: __ normal_iterator&LT; _Iterator, _Container&gt; :: operator * with _Iterator = Node *,_ _Container = std :: vector&gt; == __val' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:186:错误:不匹配'operator =='in “__first .__ gnu_cxx :: __ normal_iterator&LT; _Iterator, _Container&gt; :: operator * with _Iterator = Node *,_ _Container = std :: vector&gt; == __val' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:194:错误:不匹配'operator =='in “__first .__ gnu_cxx :: __ normal_iterator&LT; _Iterator, _Container&gt; :: operator * with _Iterator = Node *,_ _Container = std :: vector&gt; == __val' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:198:错误:不匹配'operator =='in “__first .__ gnu_cxx :: __ normal_iterator&LT; _Iterator, _Container&gt; :: operator * with _Iterator = Node *,_ _Container = std :: vector&gt; == __val' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:202:错误:不匹配'operator =='in “__first .__ gnu_cxx :: __ normal_iterator&LT; _Iterator, _Container&gt; :: operator * with _Iterator = Node *,_ _Container = std :: vector&gt; == __val'
有谁知道如何开始分析此错误?我正在寻找行号或其他信息并从那里开始。
Edit2:结束了完全不同的事情。 C ++中的模板错误,我的节点类型实现了==。我最终将我的访问变量更改为整数向量的向量。这是一个黑客和解决方法,但它将适合我的需要。
答案 0 :(得分:2)
C ++中的模板错误是所有编程中最糟糕的一些。话虽如此,你肯定需要通过引用传递Next,但模板错误来自其他地方。在这种情况下,您需要为您的Node类型实现operator==
。根据错误消息解密的唯一快速方法是看到它正在抱怨no match for 'operator==' in ...
,不幸的是,对于我们凡人而言,其余的错误消息并不真正有用,因为错误实际上是在C ++标准库的内容,它实际上试图在operator==
类型上使用Node
,但这个地方只是非常勇敢的去。