c++ Public member variable is inaccessible - c++

There's an error message "member ... is inaccessible" in Visual Studio when I try to access the value of a member variable.
But, the member variable is declared public. It's a derived class, but it's not a member variable of the base class.
From the compiler, there's an error message "cannot access protected member".
The lines that cause the error:
CKaltestDlg dlg;
fprintf(debugout, "Reminders get input focus %s \n", dlg.m_ReminderInputFocus ? "true" : "false");
The header file. The member variable in question is near the end, under a public: heading.
// KaltestDlg.h : header file
//
#pragma once
// CKaltestDlg dialog
class CKaltestDlg : public CDialogEx
{
// Construction
public:
CKaltestDlg(CWnd* pParent = nullptr); // standard constructor
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_KALTEST_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnBnClickedRemindersinputfocus();
CButton m_EnableDisableInputFocus;
BOOL m_ReminderInputFocus;
CButton m_EnableDisableRemindersOntop;
BOOL m_RemindersOnTop;
afx_msg void OnBnClickedRemindersalwaysontop();
CButton m_EnableDisableFlash;
BOOL m_FlashTaskbarButton;
afx_msg void OnBnClickedReminderflash();
};

From the documentation of the DECLARE_MESSAGE_MAP() macro
Note
If you declare any member after DECLARE_MESSAGE_MAP, you must specify a new access type (public, private, or protected) for them.
So the macro expansion may result in the visibility being changed. Either move the macro to the end of the class or add public: right after it. I'd recommend leaving a comment about this in your code to remind you of this fact if you modify the class in the future.

Related

How to implement #pragma pointers_to_members() for just one class?

I want to use #pragma pointers_to_members() to just one class.
I have other class definitions in my header file but I want to apply the required pragma to only one class and other classes should not be affected. How can this be achieved? There are not many code examples for that.
I have resolved this issue. I used the # pragma directive just before the BEGIN_MESSAGE_MAP() in my .cpp file.
#pragma pointers_to_members(full_generality,virtual_inheritance)
BEGIN_MESSAGE_MAP(CMy4407Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CMy4407Dlg::OnBnClickedButton1)
END_MESSAGE_MAP()
This is my derived class CMy4407Dlg:
class CMy4407Dlg : public CDialogEx, public virtual ABCD
{
// Construction
public:
CMy4407Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MY4407_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
};

MFC SDI project want to call GetDocument() function in a view

Now I create a totally new SDI project
the view provides a function: GetDocument(), it helps me to get the current document's data
However, When I call the GetDocument() function,VC tells me some error occurs:Debug Assertion Failed
the following is my setting
class CHorse_programView : public CView
{
protected: // create from serialization only
CHorse_programView();
DECLARE_DYNCREATE(CHorse_programView)
// Attributes
public:
CHorse_programDoc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHorse_programView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CHorse_programView();
CHorse_programDoc * GetDoc()
{
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
return (CHorse_programDoc *) pFrame->GetActiveDocument();
}
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CHorse_programView)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
and I want to call GetDocument() in this function
CHorse_programView::CHorse_programView()
{
GetDocument();
}
what's wrong
The CDocument and CView are not connected yet at CView construction time. You can move your code to OnInitialUpdate in the view to get full capability.
In the view's constructor, it hasn't been assigned to a document yet - that comes later.

How to get handler(HWND) for dialog box

Hi I have created a dialog box and it woks.
My question is: how do you retreive the handle for it?
Also, if you get the handle, how would you change the static text control text inside it?
class CStatisticsDlg : public CDialogEx
{
public:
CStatisticsDlg();
// Dialog Data
enum { IDD = IDD_STATISTICS };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
};
CStatisticsDlg::CStatisticsDlg() : CDialogEx(CStatisticsDlg::IDD)
{
}
void CStatisticsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CStatisticsDlg, CDialogEx)
END_MESSAGE_MAP()
Assuming you're using MFC (as indicated by the tag), then presumably you have a CDialog class instance. CDialog is a subclass of CWnd, so you can retrieve the window handle by one of 3 ways:
Directly accessing its m_hWnd member
Casting it to an HWND with operator HWND()
Calling GetSafeHwnd() on it
Here is how to do it.
First create a member function to the main application class.
Then use the following code (Assuming the class name is CGenericApp, and your Dialog class is CGenericDlg.
CWnd* CGenericApp::GetDlg()
{
return m_pMainWnd;
}
Then when you want to get a handler to the main Dialog box, use:
CGenericApp* app = (CGenericApp*)AfxGetApp();
CGenericDlg* pDlg = (CGenericDlg*)(app->GetDlg());
HWND win = pDlg->GetSafeHwnd();
'win' will hold the HWND you are looking for.

CMFCButton.SetToolTip() crash

I'm trying to display a tooltip on a CMfcButton.
When my code run the SetToolTip(), the application crash.
BOOL CGenerationDlg::OnInitDialog()
{
BOOL bret = CPropertyPage::OnInitDialog();
m_pButtonExport = (CMFCButton *)GetDlgItem(IDC_BTN_EXPORTE_BILAN);
m_pButtonExport->EnableFullTextTooltip();
m_pButtonExport->SetTooltip(L"my tooltip");
return bret;
}
void CKenoDlg::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
}
Here is my Header file :
// KenoDlg.h : fichier d'en-tête
//
#pragma once
#include "keno.h"
#include "AboutDlg.h"
// boîte de dialogue CKenoDlg
class CKenoDlg : public CPropertyPage
{
// Construction
public:
CKenoDlg(CWnd* pParent = NULL); // constructeur standard
CAboutDlg* myDialog;
// Données de boîte de dialogue
enum { IDD = IDD_KENO_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // Prise en charge de DDX/DDV
CMFCButton * m_pButtonExport;
// Implémentation
protected:
HICON m_hIcon;
// Fonctions générées de la table des messages
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedBtnGenerate();
afx_msg void OnBnClickedBtnExport();
afx_msg void OnStnClickedStaticAbout();
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
Here is my error :
Any idea please ?
Thanks a lot :)
Best regards,
Try this:
Change your header to define a CMFCButton instance rather than a pointer:
class CKenoDlg : public CPropertyPage
{
// ... existing code ...
protected:
virtual void DoDataExchange(CDataExchange* pDX); // Prise en charge de DDX/DDV
CMFCButton m_pButtonExport;
// ... existing code ...
};
Then change your DoDataExchange function as follows:
void CKenoDlg::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BTN_EXPORTE_BILAN, m_pButtonExport);
}
Finally, in OnInitDialog, do something like this:
BOOL CGenerationDlg::OnInitDialog()
{
BOOL bret = CPropertyPage::OnInitDialog();
m_pButtonExport.EnableFullTextTooltip();
m_pButtonExport.SetTooltip(L"my tooltip");
return bret;
}
The reason why you need to do it like this rather than with a pointer is because MFC has to subclass the control to a CMFCButton rather than the default CButton. When you use the DDX macro in DoDataExchange, this is done behind the scenes when the default implementation of OnInitDialog calls UpdateData, which in turn calls DoDataExchange and - if I recall correctly - on the first time through, the dialog controls are sub-classed to the correct types.
As you used a pointer, and had no link between the button control and the type you were using it as, there was a mismatch between the actual and expected types and that was the reason for the crash.
If you use the VS2012 wizard to add a variable from the dialog designer (right-click on a dialog control and choose Add Variable), it will create the member variable declaration in the header file and will add the DDX macro to the DoDataExchange function for you. You can then choose to change the type of the member variable, e.g. from CButton to one of your own CButton derived classes.
You use the term pointer to button and name the variable 'm_pButtonExport'. That is incorrect. The code creates an instance of a CMFCButton object, not a pointer to an instance of a button object. The variable should be named 'm_ButtonExport' and referred to as an instance of a CMFCButton object NOT a pointer to one.

OnSelectionChanged not getting called

Header:
#pragma once
class AlarmsList : public CVSListBox
{
DECLARE_DYNAMIC(AlarmsList)
public:
AlarmsList();
virtual ~AlarmsList();
void OnAfterAddItem(int index);
void OnSelectionChanged(NMHDR *pNMHDR, LRESULT *pResult);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnDtnDatetimechangeDatetimepicker1(NMHDR *pNMHDR, LRESULT *pResult);
};
void AlarmsList::OnAfterAddItem(int index)
{
GetParent()->GetDlgItem(IDC_TIMEPICK)->EnableWindow(true);
LOGIC->addAlarm();
LOGIC->changeSelection(index);
}
void AlarmsList::OnSelectionChanged(NMHDR *pNMHDR, LRESULT *pResult)
{
}
OnAfterAddItem gets called when i add a new item but OnSelectionChanged NEVER gets called how much i even try.
Linking it trough a message map neither dosnt work:
IMPLEMENT_DYNAMIC(AlarmsList, CVSListBox)
BEGIN_MESSAGE_MAP(AlarmsList, CVSListBox)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST, OnSelectionChanged)
END_MESSAGE_MAP()
I create the AlarmsList object using the create function.
Source code and project: http://www.filedropper.com/clockmaster
Generally, I think the LVN_ITEMCHANGED notification is sent to the parent window. Put the handler and the message map entry int your dialog/window that is the parent of the list box.
Didnt help :/.
Tried both parent property page and that property pages dialog.
Overloading dosnt work either :/, it does for OnAfterAddItem tough.
And yes I'm then using the same parameters as the virtual function.
You can try overriding the functions in the CVSListBoxBase class.In this class, the signature of OnSelectionChanged function requires no arguments.
You can find the declaration of the CVSListBoxBase class in afxvslistbox.h.
Just had a look at some of my own MFC code that uses list boxes, and the following works;
CMyListBox : public CListBox
{
}
class CMyDialog : public CDialog
{
// Construction
public:
CMyDialog(CFeatureDoc* pFeatureDoc,BOOL SheetLayout = FALSE,CWnd* pParent = NULL); // standard constructor
//{{AFX_DATA(CMyDialog)
enum { IDD = IDD_MY_DIALOG };
CMyListBox m_MyListBox;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CMyDialog)
afx_msg void OnSelChangeListBox();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDialog)
DDX_Control(pDX, IDC_MY_LIST_BOX, m_MyListBox);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
ON_LBN_SELCHANGE(IDC_MY_LIST_BOX, OnSelChangeListBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDialog message handlers
void CMyDialog::OnSelChangeListBox()
{
}
If you want to have your own control process messages from a dialog, you may want to subclass it. See this related question What's the correct way to create a subclass of a MFC control?