Friday, 22 September 2017

Simple C++ Pointers: How to link a child node to parent node

I have a very simple node structure I am using to implement Iterative Deepening DFS. However, I later run into trouble linking a child node to a parent node.



struct Node

{
std::vector 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;
}
};



Later on in my code I get harsh errors when I try to do something like this:



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 frontier;
std::vector 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 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 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);
}
}
}

}


All I want to do is link this child node (called Down_Node) to it's parent node (called Next). BUT how can I do this if Next is not a Node* itself?



It's that pointer to Next that is giving trouble. I tried &(Next), *Next, etc but couldn't get this to work. I tried making a Node pointer variable that points to Next, but again I couldn't get this to work. I'm trying to figure this out but having a lot of trouble. It's my misunderstanding on pointers in C++ that is leading to my downfall.



Edit: When I try to use &Next to pass the reference, I get a huge garbled error that I am not understanding.





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’





Does anyone know how to begin analyzing this error? I am looking for line numbers or other info in it and going from there.




Edit2: Ended up being something completely different. Template errors in C++ with implementing == for my Node type. I ended up changing my visited variable to a vector of vector of ints. It's a hack and a workaround, but it will suit what I need.

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...