0
votes

I try to print values from my object class, but I am unable to properly access the information stored at the pointer. Below I have defined a simple struct.

When compiled, I get an error:

no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::vector<int>')

void PrintNode(Node *node) { cout << node->key << endl; }

struct Node
{
    vector<int> key;
    int parent; 
    Node(vector<int> x, int y){ key = x; parent = y; }
    void PrintNode(Node* node) { cout << node->key << endl; }
};

I call my PrintNode in my BFS function:

void BFS( vector<int> permutation, int n ) {
    vector<Node*>Pointers;
    queue<Node*> Queue;
    Node* start = new Node(permutation, -1);
    Node::PrintNode( start );
    Pointers.push_back( start );
}

I don't understand why I am unable to cout the integer vector stored in .key of the node object. I believe that I am dereferencing the pointer correctly with node->key.

2

2 Answers

1
votes

The standard library doesn't support direct iostreams output of a vector. But you can easily define such an operation. Just do it with a loop.

0
votes

std::cout cannot handle raw vectors, you must convert it to an array which is can process first. You can do this using vector's .data() method

Example:

void PrintNode(Node* node) { cout << node->key.data() << endl; }