【问题标题】:Print Object that is pointed to打印指向的对象
【发布时间】:2019-03-02 20:37:49
【问题描述】:

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

编译时出现错误:

'operator}' 和 'std::vector')

void PrintNode(Node *node) { cout key

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 中的整数向量。我相信我使用node-&gt;key 正确地取消引用了指针。

【问题讨论】:

    标签: c++ struct function-pointers


    【解决方案1】:

    标准库不支持vector 的直接 iostreams 输出。但是您可以轻松定义这样的操作。只需使用循环即可。

    【讨论】:

    • 感谢您的评论。这确实是导致该错误的原因。同样对于在另一个错误之后看到此代码的任何人来说都是“Node::PrintNode(start);”应该是“start->PrintNode();”并删除打印函数的参数。
    【解决方案2】:

    std::cout 无法处理原始向量,您必须将其转换为可以首先处理的数组。你可以使用vector.data()方法来做到这一点

    例子:

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

    【讨论】:

      【解决方案3】:

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

      这个问题的简单答案是std::vector&lt;type, allocator&gt; 没有std::ostream::&lt;&lt; 运算符的重载。因此,当您尝试打印出整个键向量时,它不会按您期望的方式工作。我在其他帖子上看到了几个答案,这些答案建议为std::vector 重载&lt;&lt; 运算符,但除非您知道自己在做什么,否则出于多种原因我会避免这样做,其中一个是全局命名空间污染,第二个是不正确的处理重载本身。

      另外,请停止using namespace std;。它不会以任何方式帮助您,只会以最意想不到的方式使事情变得更糟。

      这里有一些可能会有所帮助的修复。

      第 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";
          }
      };
      

      第 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.
      }
      

      也就是说,您发布的代码摘录毫无意义。我刚刚为您提供的部件提供了修复。不保证它可以在您手头的更大范围内工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多