简体   繁体   English

指向的打印对象

[英]Print Object that is pointed to

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>') 'operator<<' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char>}' 和 'std::vector<int>')

void PrintNode(Node *node) { cout << node->key << endl; 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:我在我的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 );
}

I don't understand why I am unable to cout the integer vector stored in .key of the node object.我不明白为什么我无法cout存放在整数向量.key节点的对象。 I believe that I am dereferencing the pointer correctly with node->key .我相信我正在使用node->key正确取消引用指针。

The standard library doesn't support direct iostreams output of a vector .标准库不支持vector直接 iostreams 输出。 But you can easily define such an operation.但是您可以轻松定义这样的操作。 Just do it with a loop.只需使用循环即可。

std::cout cannot handle raw vectors, you must convert it to an array which is can process first. std::cout无法处理原始向量,您必须将其转换为可以首先处理的数组。 You can do this using vector 's .data() method您可以使用vector.data()方法执行此操作

Example:例子:

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

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.下面我定义了一个简单的结构。

The simple answer to this question is the fact that std::vector<type, allocator> does not have an overload for the std::ostream::<< operator.这个问题的简单答案是std::vector<type, allocator>没有std::ostream::<<运算符的重载。 Hence when you try to print out the entire vector of keys, it won't work the way you expect it to.因此,当您尝试打印出整个键向量时,它不会以您期望的方式工作。 I have seen several answers on other posts which suggest overloading the << operator for std::vector but unless you know what you are doing I would avoid doing this for several reasons, one of them being global namespace pollution and the second being incorrect handling of the overloading itself.我在其他帖子上看到了几个答案,这些答案建议为std::vector重载<<运算符,但除非您知道自己在做什么,否则我会出于多种原因避免这样做,其中一个是全局命名空间污染,第二个是不正确的处理重载本身。

Also, please stop doing using namespace std;另外,请停止using namespace std; . . It will not help you in any way and just make things worse in the most unexpected ways.它不会以任何方式帮助您,只会以最意想不到的方式使事情变得更糟。

Here are some fixes which may help.这里有一些可能会有所帮助的修复方法。

Part 1 - Node struct第 1 部分 - 节点结构

struct Node : public std::enable_shared_from_this<Node>
{
    std::vector<int> keys;
    int parent; 
    Node(vector<int> x, int y) : keys(x), parent(y){}
    Node(const Node& rhs): keys(rhs.keys), parent(rhs.parent) {}
    Node(Node&& rhs) noexcept: keys(std::move(rhs.keys)), parent(rhs.parent){}
    
    void PrintNode()
    {
        for (auto& key : node->keys)
            cout << key << "\n";
    }
};

Part 2 BFS Code第 2 部分 BFS 代码

void BFS(std::vector<int>& permutation, int n )
{
    /* I don't see the real value in creating pointers for your case. You can easily live with an instance of the class Node. This also gives you scoped initialization as the pointers vector goes out of scope, your nodes will get deallocated too. at least in the context, you have posted above, that seems desirable.
However, if you insist on creating pointers, you can use smart pointers.
*/
    std::vector<std::shared_ptr<Node>> pointers;
    std::queue<std::shared_ptr<Node>> queue; // not used??
    auto start = std::make_shared<Node>(permutation, -1); // make a shared pointer
/* PrintNode in your code is an instance level function. Invoke it using the scope resolution operators . or ->. If you insist on doing it your way, then declare the function static. However, that has its own quirks and you need to understand static functions before you do this. */
    start->PrintNode(); 
    pointers.push_back(std::move(start)); // add your pointer to the vector.
}

That said the code excerpt you have posted makes little sense.也就是说,您发布的代码摘录毫无意义。 I have just provided fixes for the parts you have provided.我刚刚为您提供的部分提供了修复程序。 Does not guarantee that it will work in the larger context you may have at hand.不保证它会在您手头可能拥有的更大范围内工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM