c++mfcmodeless-dialog

no instance of overloaded function matches the argument list c++ - Create Modeless dialog


I'm trying to create a modeless dialog from resource by using Create() and ShowWindow() function. But it end up getting error "no instance of overloaded function matches the argument list c++" Here is my Appication command.cpp

void
CSurveyApp::OnAskAI()
{
    //MessageBox()
    /*dlg_AskAI dlg_AskAI;
    dlg_AskAI.DoModal();*/
    dlg_AskAI m_dialog;
    if (!m_dialog) {
        m_dialog.Create(IDD_ASKAI, this);
        m_dialog.ShowWindow(SW_SHOW);
    }
    
    
}

Here is dialog.h file

#pragma once
#include "afxdialogex.h"


// dlg_AskAI dialog

class dlg_AskAI : public CDialog
{
    DECLARE_DYNAMIC(dlg_AskAI)

public:
    dlg_AskAI(CWnd* pParent = nullptr);   // standard constructor
    virtual ~dlg_AskAI();

// Dialog Data
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_ASKAI };
#endif

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    DECLARE_MESSAGE_MAP();
public:
    afx_msg void OnBnClickedButton2();
    afx_msg void OnEnChangeEdit3();
    afx_msg void OnBnClickedButton3();
    afx_msg void OnEnChangeEdit4();
    afx_msg void OnClose();
private:
    CString m_message;

};

Here is dialog.cpp file

// dlg_AskAI.cpp : implementation file
//

#include "StdAfx.h"
#include "afxdialogex.h"
#include "resource.h"
#include "dlg_AskAI.h"


// dlg_AskAI dialog

IMPLEMENT_DYNAMIC(dlg_AskAI, CDialog)

dlg_AskAI::dlg_AskAI(CWnd* pParent /*=nullptr*/)
    : CDialog(IDD_ASKAI, pParent)
    , m_message(_T(""))
{

}
CString m_message;
dlg_AskAI::~dlg_AskAI()
{
}

void dlg_AskAI::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT3, m_message);
}


BEGIN_MESSAGE_MAP(dlg_AskAI, CDialog)
    ON_BN_CLICKED(IDC_BUTTON2, &dlg_AskAI::OnBnClickedButton2)
    ON_EN_CHANGE(IDC_EDIT3, &dlg_AskAI::OnEnChangeEdit3)
    ON_BN_CLICKED(IDC_BUTTON3, &dlg_AskAI::OnBnClickedButton3)
    ON_BN_CLICKED(IDC_EDIT4, &dlg_AskAI::OnEnChangeEdit4)
END_MESSAGE_MAP()


void dlg_AskAI::OnClose()
{
    DestroyWindow();
}
//Contact Us
void dlg_AskAI::OnBnClickedButton2()
{
    // TODO: Add your control notification handler code here
}


void dlg_AskAI::OnEnChangeEdit3()
{
    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO:  Add your control notification handler code here

    
}

//Send Button
void dlg_AskAI::OnBnClickedButton3()
{
    
}


void dlg_AskAI::OnEnChangeEdit4()
{
  
}

it end up with this error

no instance of overloaded function "dlg_AskAI::Create" matches the argument list

First time asking, please go easy on me. Note that, I have very minimal experience on C++ please help with heart T.T


Solution

  • second argument of CDialog::Create should be CWnd* not CApp*, you should leave the second argument empty which defaults it to null.

    you have a problem with the lifetime of the dialog, modeless dialogs (unlike modal ones) cannot be created on the stack. (because 1. they outlast the stack and 2. they should delete themselves when they are closed).

    to fix this, you need to

    1. have a member pointer to the window in your CApp object. (optional)
    2. allocate the dialog with new, and store it in that member.
    3. follow MSDN documentation on modelss dialogs cleanup in the remarks

    When you implement a modeless dialog box, always override the OnCancel member function and call DestroyWindow from within it. Don't call the base class CDialog::OnCancel, because it calls EndDialog, which will make the dialog box invisible but will not destroy it. You should also override PostNcDestroy for modeless dialog boxes in order to delete this, since modeless dialog boxes are usually allocated with new. Modal dialog boxes are usually constructed on the frame and do not need PostNcDestroy cleanup.