将shared_ptr与指向指针的指针一起使用时发生编译器错误 [英] Compiler error while using shared_ptr with a pointer to a pointer

查看:57
本文介绍了将shared_ptr与指向指针的指针一起使用时发生编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to using smart pointers in C++ and my current issue is that I am converting C code to C++ (C++11/14/17) and I am having some problems understanding using shared_ptr with a pointer to pointer. I have derived a toy example which I believe illustrates the problem

以下是头文件

#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

And the actual code 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;
}

我遇到了这些编译器错误-

I am getting these compiler errors -

 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上)

g++ version is 7.3.0 (on Ubuntu 18)

我需要使用C ++样式转换C样式malloc分配的帮助,该C ++样式要求使用指向指针的指针,然后解决如何解决编译器错误.如果我似乎缺少明显的东西,我深表歉意.

I need help converting the C style malloc allocation with a C++ style calling for a pointer to a pointer and then how to fix the compiler errors. My apologies if it looks like I am missing something obvious.

推荐答案

如果此 ProxyNode ** flane_pointers; 应该是 ProxyNode 的矩阵,则最简单方法将是创建包含 ProxyNode 的矢量的矢量,如下所示:

If this ProxyNode** flane_pointers; is supposed to be a matrix of ProxyNode then the most simple approach would be to create vector of vectors containing ProxyNode like this:

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>();
}

如果您确实需要使用指向指针的指针(但我确实不知道这样做的目的),则可以执行以下操作:

If you really need the pointer to pointer (but I really don't see the purpose for that) you can do:

    // 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 的行为与原始指针不完全相同,例如,您不应该使用 operator new []为 shared_ptr 分配内存..在此说明了原因.

Note that std::shared_ptr does not behave exactly as raw pointer, for example you shouldn't allocate the memory for shared_ptr with operator new[]. Here is explained why.

我还看到您可能想要实现某种列表或其他链. std :: shared_ptr 可能会遭受,因此请小心并在需要时使用 std :: weak_ptr .

Also I see that you probably want to implement some kind of list or other chain. std::shared_ptr can suffer from cyclic dependency, so be careful and use std::weak_ptr when needed.

这篇关于将shared_ptr与指向指针的指针一起使用时发生编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆