0
votes

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<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;
}
};

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<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);
    }       
    }
}
}

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.

1
What is Down_Child? That will try to initialize config, correct?Neil Kirk
Down_Child has to be of type std::vector<int>. And pass &Next.a_pradhan
Down_Child already exists at that point in the code. It is simply a vector of ints.user2055216
Here is an example : cpp.sh/57qm. Also, I didnt initialize Next node.a_pradhan
When I pass by &Next, I get a huge error. I will post it above.user2055216

1 Answers

2
votes

Template errors in C++ are some of the nastiest in all of programming. With that said, you definitely need to pass Next by reference, but the template error is coming from somewhere else. It looks like in this case you need to implement operator== for your Node type. The only quick way to decipher that based on the error message is to see that it is complaining about that no match for 'operator==' in ..., unfortunately for us mere mortals the rest of that error message is not really useful because the error actually get raised in the bowels of the C++ standard libraries where it actually tries to use operator== on the Node type, but that a place only the very brave go.