我尝试从我的对象类打印值,但我无法正确访问存储在指针中的信息。下面我定义了一个简单的结构。
编译时出现错误:
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; }
};
我在我的 BFS
函数中调用了我的 PrintNode
:
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 );
}
我不明白为什么我无法cout
存储在节点对象的.key
中的整数 vector 。我相信我正在使用 node->key
正确取消引用指针。
最佳答案
标准库不支持 vector
的直接 iostreams 输出。但是您可以轻松定义这样的操作。只需循环执行即可。
关于c++ - 打印指向的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52528365/