以共享指针作为参数创建对象的共享指针

时间:2016-12-20 11:59:34

标签: c++ object shared-ptr

尝试创建一个以std::shared_ptr作为参数的对象的std::shared_ptr导致“没有匹配的构造函数来初始化'object'”编译错误。

我的代码如下:

// Sidebar Widgets
  _widgets.sideBarWidgetLeft = std::make_shared<SideBarWidget>();
  _widgets.sideBarWidgetLeft->hide();
  _widgets.sideBarWidgetRight = std::make_shared<SideBarWidget>();
  _widgets.sideBarWidgetRight->hide();

  // Pointer Test
  CalibrationManagementWidget* test = new CalibrationManagementWidget( _widgets.sideBarWidgetLeft, 0 );

  // Sidebar Left
  _widgets.calibrationManagementWidget = std::make_shared<CalibrationManagementWidget>( _widgets.sideBarWidgetLeft, 0 );
  _widgets.calibrationManagementWidget->hide();
  _widgets.cameraWidget = std::make_shared<CameraWidget>( 0, VISUALISATION_TYPE_NORMAL );
  _widgets.cameraWidget->hide();

虽然“指针测试”编译得很好,但“侧边栏左侧”下的线条会产生命名错误。有没有什么我做错了实例化或一般不可能这样做?

这是(一个)错误消息:

/usr/include/c++/5.4.0/ext/new_allocator.h:120: Error: no matching constructor for initialization of 'CalibrationManagementWidget'
        { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
                             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~

修改

根据要求,我会发布一些片段,其中也包含CalibrationWidget的构造函数。

calibrationwidget.h

...
Q_INVOKABLE CalibrationManagementWidget(QWidget* parent = 0 , VisualisationType visuType = VISUALISATION_TYPE_TRANSLATOR);
CalibrationManagementWidget( std::shared_ptr<SideBarWidget> sideBarWidget, QWidget* parent = 0 );
virtual ~CalibrationManagementWidget();
...

calibrationwidget.cpp

...
CalibrationManagementWidget::CalibrationManagementWidget(QWidget* parent, enum VisualisationType visuType) : CommonWindowWidget( WIDGET_TYPE_3D, "", "Calibration Management", 0, Ui::UI_WINDOW_FLAG_CLOSABLE, 0, parent, visuType) {
  _currentMessage = 0;
  _selectedCalibrationType = CALIBRATION_TYPE_UNKNOWN;
  _messageEventLoop = new QEventLoop(this);
  _updateTimer = new QTimer();
  setupUi();
}


CalibrationManagementWidget::CalibrationManagementWidget(std::shared_ptr<SideBarWidget> sideBarWidget, QWidget* parent) : CommonWindowWidget( WIDGET_TYPE_3D, "", "Calibration Management", 0, Ui::UI_WINDOW_FLAG_CLOSABLE, sideBarWidget, parent) {
  _currentMessage = 0;
  _selectedCalibrationType = CALIBRATION_TYPE_UNKNOWN;
  _messageEventLoop = new QEventLoop(this);
  _updateTimer = new QTimer();
  setupUi();
}

CalibrationManagementWidget::~CalibrationManagementWidget() {
  delete _messageEventLoop;
  _messageEventLoop = 0;
  delete _updateTimer;
  _updateTimer = 0;
}
...

以下是CommonWindowWidget的片段。

commonwindowwidget.h

...
CommonWindowWidget(WidgetType widgetType, const QString& headerTitle = "Foldable Widget", const string& pageName = "Foldable Widget", unsigned int view = 0, unsigned int uiWindowFlags = 0, std::shared_ptr<SideBarWidget> sideBarWidget = 0, QWidget* parent = 0, VisualisationType visuType = VISUALISATION_TYPE_NORMAL);
    virtual ~CommonWindowWidget();
...

commonwindowwidget.cpp

...
CommonWindowWidget::CommonWindowWidget(WidgetType widgetType, const QString& headerTitle, const string& pageName, unsigned int view, unsigned int uiWindowFlags, std::shared_ptr<SideBarWidget> sideBarWidget, QWidget* parent, VisualisationType visuType) : CommonStackedWidget(widgetType, pageName, view, parent, visuType) {
  _windowFlags = uiWindowFlags;
  _sideBarWidget = std::move(sideBarWidget);

  setupUi();

  _ui.headerWidget->headerLabel()->setText( headerTitle );

  connect( _ui.headerWidget, SIGNAL(headerDoubleClicked()), this, SLOT(changeFoldState()));
  connect( _ui.headerWidget, SIGNAL(changeFoldStateClicked()), this, SLOT(changeFoldState()));
  connect( _ui.headerWidget, SIGNAL(closeClicked()), this, SLOT(navigateToHome()));
}

CommonWindowWidget::~CommonWindowWidget() {

}
...

1 个答案:

答案 0 :(得分:5)

转发时,0会导致参数推导为int,不再可转换为指针。

只有 literal 0可以表示指针,但在传递给make_shared后,文字变为int值0,导致错误,如构造函数需要一个指针。

改为使用nullptr

_widgets.calibrationManagementWidget =
   std::make_shared<CalibrationManagementWidget>(
     _widgets.sideBarWidgetLeft, nullptr
   );

这是nullptr被引入的主要原因。

说明:https://channel9.msdn.com/Shows/Going+Deep/Stephan-T-Lavavej-Everything-you-ever-wanted-to-know-about-nullptr,大约28:00。