【问题标题】:How to initialize shared pointers in constructor?如何在构造函数中初始化共享指针?
【发布时间】:2021-07-03 23:31:15
【问题描述】:

我正在尝试将构造函数中其他类的对象初始化为共享指针。我需要一个 shred 指针,因为我需要在 ...

中的另一个方法中使用的引用

标题

class MyClass
{
public:
   MyClass() ;
   ~MyClass() {};
   void myMethod();
private:
   std::shared_ptr<dds::pub::Publisher>m_pub;
   std::shared_ptr<dds::domain::DomainParticipant>m_part;
};

cpp

MyClass::MyClass()  
{
  m_part = std::make_shared<dds::domain::DomainParticipant>(domain::default_id());
  m_pub = std::make_shared<dds::pub::Publisher>(m_part);
}
MyClass::myMethod()
{
  //m_part, m_pub are used here 
}

我在这里错过了什么?

Error   C2039   'delegate': is not a member of 'std::shared_ptr<dds::domain::DomainParticipant>'     

dds::pub::Publisher

namespace dds
{
  namespace pub
  {
     typedef dds::pub::detail::Publisher Publisher;
  }
}

出版商

namespace dds { namespace pub { namespace detail {
typedef 
dds::pub::TPublisher<org::eclipse::cyclonedds::pub::PublisherDelegate> Publisher;
} } }

PublisherDelegate

namespace dds { namespace pub { namespace detail {
    typedef 
dds::pub::TPublisher<org::eclipse::cyclonedds::pub::PublisherDelegate> Publisher;
} } }


class OMG_DDS_API PublisherDelegate : public 
org::eclipse::cyclonedds::core::EntityDelegate
{
public:
typedef ::dds::core::smart_ptr_traits< PublisherDelegate >::ref_type ref_type;
typedef ::dds::core::smart_ptr_traits< PublisherDelegate >::weak_ref_type weak_ref_type;

PublisherDelegate(const dds::domain::DomainParticipant& dp,
                  const dds::pub::qos::PublisherQos& qos,
                  dds::pub::PublisherListener* listener,
                  const dds::core::status::StatusMask& event_mask);

TPublisher

template <typename DELEGATE>
class dds::pub::TPublisher : public dds::core::TEntity<DELEGATE>
{
public:

   typedef dds::pub::PublisherListener                 Listener;

public:
   OMG_DDS_REF_TYPE_PROTECTED_DC(TPublisher, dds::core::TEntity, DELEGATE)
   OMG_DDS_IMPLICIT_REF_BASE(TPublisher)

   TPublisher(const dds::domain::DomainParticipant& dp);

   TPublisher(const dds::domain::DomainParticipant& dp,
           const dds::pub::qos::PublisherQos& qos,
           dds::pub::PublisherListener* listener = NULL,
           const dds::core::status::StatusMask& mask = dds::core::status::StatusMask::none());

我尝试了答案中给出的方法得到了新的错误,

Error   C2672   'std::dynamic_pointer_cast': no matching overloaded function  in TPublisher.hpp   

【问题讨论】:

  • TPublisher 没有采用shared_ptr 的构造函数,所以我预计会有不止一个错误。 (您使用shared_ptr 的动机很奇怪,而且似乎被切断了。)另外,请查看输出窗口中的完整错误消息,而不是使用错误列表。

标签: c++ visual-studio constructor initialization make-shared


【解决方案1】:

我猜m_pub应该这样初始化

m_pub = std::make_shared<dds::pub::Publisher>(*m_part);

dds::pub::Publisher 又名dds::pub::TPublisher 类的构造函数通过引用获取const dds::domain::DomainParticipant

问题更新后答案已更改。

【讨论】:

  • 我添加了一些代码sn-ps供您参考......它的cyclonedds代码库
  • 仍然没有 Publisher 构造函数(现在这是 TPublisher)。无论如何,我认为我的修复是正确的。试试看。
  • 我尝试了答案中给出的方法得到了一个新错误,错误 C2672 'std::dynamic_pointer_cast': TPublisher.hpp 中没有匹配的重载函数
  • 没有足够的上下文来帮助你。请尝试制作minimal reproducible example,这对您来说很难,但在制作过程中您可能会找到解决方案。
猜你喜欢
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多