c++ - 打印指向的对象

标签 c++ struct function-pointers

我尝试从我的对象类打印值,但我无法正确访问存储在指针中的信息。下面我定义了一个简单的结构。

编译时出现错误:

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/

相关文章:

objective-c - 如何将 Swift 结构作为参数传递给 Objective-C 方法

在结构中使用函数指针数组时崩溃

php - 函数作为数组值

c - 理解结构中的结构

c - C中的跳转表数组

c++ - 散列<std::string> 与散列<std::string_view>

c++ - 嵌套匿名命名空间

java - 获取 PageRank

c++ - 不能使用线程构造函数

c - 字符串在执行中途丢失数据 C