0

I need to print the array from nodes in a list that I made for a heuristic search algorithm.

For the list, I have a C++ map std::map<int, Node>List

WHERE Node is a structure that looks like:

struct Node{
    Node *pParent;
    int array[3][3];  };

I can successfully insert the Node "n" to the list like this:

List.insert(std::make_pair(0, *n));

But I'm having trouble on how to send "Node" as a parameter to the function:

void printArr(Node *tNode){
int row, col;
//prints the 2Darray
    for(row = 0; row < 3; row++) {
        for(col = 0; col < 3; col++) {
            std::cout << tNode->array[row][col] << " ";
 }

I tried this method to send it as parameter but it did not work.

std::map<int, Node> p = List.begin();   
printBoard(p.second);

I received the following warnings:

[Error] conversion from 'std::map<int, Node>::iterator {aka std::_Rb_tree_iterator<std::pair<const int, Node> >}' to non-scalar type 'std::map<int, Node>' requested

[Error] 'class std::map<int, Node>' has no member named 'second'

Also, how should I access the contents of Node "n" which is added to List?

lilyarc
  • 3
  • 1

1 Answers1

0

std::map<int, Node> p = List.begin(); doesn't work because begin() returns an iterator but you've declared p to have type std::map.

You want:

std::map<int, Node>::iterator p = List.begin(); 

Or simply use auto to not have to care about the type:

auto p = List.begin();
emlai
  • 37,861
  • 9
  • 87
  • 140
  • Thank you! Is there a way for me to send only the array as parameter? I have another function with a 2D array as parameter and I tried: 'callFunction(p->second->board);' and it doesn't work. Only the array of the node needs to be sent. Is that possible? – lilyarc Nov 02 '20 at 23:31
  • @lilyarc Check out https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – emlai Nov 03 '20 at 00:21