c++ - 将 shared_ptr 与指向指针的指针一起使用时出现编译器错误

标签 c++ compiler-errors g++ shared-ptr smart-pointers

我不熟悉在 C++ 中使用智能指针,我当前的问题是我正在将 C 代码转换为 C++ (C++11/14/17),并且我在理解将 shared_ptr 与指向指针的指针一起使用时遇到了一些问题。我推导出了一个玩具示例,我相信它可以说明问题

下面是头文件

#include <memory>
using std::shared_ptr;

struct DataNode
{
  shared_ptr<DataNode> next;
} ;


 struct ProxyNode
 {
   shared_ptr<DataNode> pointers[5];
 } ;


 struct _test_
 {
   ProxyNode** flane_pointers;
  };

和实际代码test.cpp

#include <stdint.h>
#include "test.h"


shared_ptr<DataNode> newNode(uint64_t key);

shared_ptr<ProxyNode> newProxyNode(shared_ptr<DataNode> node);

int main(void)
{
  // Need help converting this to a C++ style calling
  ProxyNode** flane_pointers = (ProxyNode**)malloc(sizeof(ProxyNode*) * 100000);
  // Here is my attempt (incomplete)
  ProxyNode** flane_pointers = new shared_ptr<ProxyNode> ?
  shared_ptr<DataNode> node = newNode(1000);
  flane_pointers[1] = newProxyNode(node)
} 

shared_ptr<ProxyNode> newProxyNode(shared_ptr<DataNode> node) 
{

  shared_ptr<ProxyNode> proxy(new ProxyNode());
 return proxy;
}

shared_ptr<DataNode> newNode(uint64_t key) 
{
 shared_ptr<DataNode> node(new DataNode());
 return node;
}

我收到这些编译器错误 -

 test.cpp: In function ‘int main()’:
 test.cpp:12:42: error: cannot convert ‘std::shared_ptr<ProxyNode>’ to  ‘ProxyNode*’ in assignment
  flane_pointers[1] = newProxyNode(node)

编译为

  g++ -c -g test.h test.cpp

g++ 版本为 7.3.0(在 Ubuntu 18 上)

我需要帮助将 C 风格的 malloc 分配转换为 C++ 风格的指针调用,然后如何修复编译器错误。如果看起来我遗漏了一些明显的东西,我深表歉意。

最佳答案

如果这个ProxyNode** flane_pointers;应该是 ProxyNode 的矩阵那么最简单的方法是创建包含 ProxyNode 的 vector vector 像这样:

struct DataNode
{
    std::shared_ptr<DataNode> next;
};


struct ProxyNode
{
    // here instead of C style array you can use std::array
    std::shared_ptr<DataNode> pointers[5];
};

TEST(xxx, yyy) {
    std::vector<std::vector<ProxyNode> > matrixOfProxyNodes;
    // insert empty vector of ProxyNode
    matrixOfProxyNodes.push_back(std::vector<ProxyNode>());
    // insert ProxyNode to the first vector of ProxyNodes
    matrixOfProxyNodes[0].push_back(ProxyNode());
    // insert shared_ptr to DataNode in position 0, 0 of matrix of ProxyNodes
    matrixOfProxyNodes[0][0].pointers[0] = std::make_shared<DataNode>();
}

如果你真的需要指向指针的指针(但我真的不明白这样做的目的)你可以这样做:

    // create shared pointer to shared pointer to ProxyNode
    // and initialize it to nullptr
    std::shared_ptr<std::shared_ptr<ProxyNode> > ptr2ptr2ProxyNode(nullptr);
    // dynamically create new shared_ptr to ProxyNode
    ptr2ptr2ProxyNode.reset(new std::shared_ptr<ProxyNode>(nullptr));
    // dynamically create ProxyNode
    ptr2ptr2ProxyNode->reset(new ProxyNode());
    (*ptr2ptr2ProxyNode)->pointers[0] = std::make_shared<DataNode>();

请注意 std::shared_ptr与原始指针的行为不完全相同,例如,您不应该为 shared_ptr 分配内存与 operator new[] . Here is explained why .

我还看到您可能想要实现某种列表或其他链。 std::shared_ptr会患cyclic dependency , 所以小心使用 std::weak_ptr需要时。

关于c++ - 将 shared_ptr 与指向指针的指针一起使用时出现编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55652188/

相关文章:

gcc - 将多个 -std 开关传递给 g++

c++ - 为什么我会收到 'multiple definition' 错误?我如何解决它?

c++ - 使用 c++0x - 安全吗?

javascript - 从 Qt 调用 Javascript 函数不显示输出

c++ - 我如何使用模板化的 typedefs,它们是_in_一个类,来自类外部(例如,由另一个类),与 boost::graph 相关

CMake: add_compile_options() 错误与 RELEASE/DEBUG 配置

c++ - 文本文件的二进制输入

c - 我正在尝试使用 C 解决给定总线数量的最大平台数量问题。我遇到错误并卡在那里

compiler-errors - 64位libuuid不兼容,编译时找不到32位libuuid

build - 构建 VRip 时缺少 tk.h 和 tcl.h 文件