Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12231

How do I add a std::unique_ptr owning a CDialogEx to a std::vector avoiding "attempting to reference a deleted function"?

$
0
0

C++ and Windows are fighting with each other, and I need to get them to stop.

I have a Visual Studio 2022 generated class that implements CDialogEx, and I need to add this to an array capable of insert. This is hard.

The class that Visual Studio 2022 generated for the dialog looks like this:

class SignPage : public CDialogEx{    DECLARE_DYNAMIC(SignPage)public:    SignPage(CWnd* pParent = nullptr);   // standard constructor    virtual ~SignPage();// Dialog Data#ifdef AFX_DESIGN_TIME    enum { IDD = IDD_SIGN_PAGE };#endifprotected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support    DECLARE_MESSAGE_MAP()};

The implementation of the class generated by Visual Studio looks like this:

// SignPage.cpp : implementation file//#include "pch.h"#include "App.h"#include "afxdialogex.h"#include "SignPage.h"// SignPage dialogIMPLEMENT_DYNAMIC(SignPage, CDialogEx)SignPage::SignPage(CWnd* pParent /*=nullptr*/)    : CDialogEx(IDD_SIGN_PAGE, pParent){}SignPage::~SignPage(){}void SignPage::DoDataExchange(CDataExchange* pDX){    CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(SignPage, CDialogEx)END_MESSAGE_MAP()// SignPage message handlers

I understand that the array capable of insert is std::vector, however inserting any Windows generated objects into std::vector results in "attempting to reference a deleted function" errors as Windows has deleted the copy and move functions in their objects.

I do not want the array capable of insert to molest the Windows provided objects in any way, and so I understand std:unique_ptr is what I need so that I add a pointer to the Windows provided objects to the list.

I instantiate the object and vector like this, and the compiler likes it:

std::vector<std::unique_ptr<SignPage>> m_dlgs;std::unique_ptr<SignPage> upm_dlg1 = std::make_unique<SignPage>();

Now I try and insert the upm_dlg1 into m_dlgs like this:

std::vector<std::unique_ptr<SignPage>>::iterator it;it = m_dlgs.begin();m_dlgs.insert(it, upm_dlg1); <---- error happens here

The addition of the last line above causes a compile error inside the template rendering unique pointers as follows:

1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\xmemory(700,82): error C2280: 'std::unique_ptr<SignPage,std::default_delete<SignPage>>::unique_ptr(const std::unique_ptr<SignPage,std::default_delete<SignPage>> &)': attempting to reference a deleted function

Does anyone know what steps I must take to fix this?

My limitations are:

  • The objects I need to add to the array capable of insert are generated by Visual Studio for the benefit of Windows. My options to modify these objects are limited.

  • The compiler error is being triggered deep inside what I understand to be the template implementing unique_ptr. I am assuming this is the joy of c++ compiler errors.

  • I am led to believe that std::vector is the collection I should be using, but this may not be the case, can anyone confirm? I have no need for sorting or any other complexity, just a simple array where I can insert and remove without in any way touching the object in the list.


Viewing all articles
Browse latest Browse all 12231

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>