【问题标题】:c++ Public member variable is inaccessiblec++公共成员变量不可访问
【发布时间】:2021-10-24 11:08:26
【问题描述】:

当我尝试访问成员变量的值时,Visual Studio 中出现错误消息“成员...不可访问”。
但是,成员变量被声明为公共的。它是一个派生类,但它不是基类的成员变量。
编译器显示错误消息“无法访问受保护的成员”。
导致错误的行:

    CKaltestDlg dlg;
        fprintf(debugout, "Reminders get input focus %s \n", dlg.m_ReminderInputFocus ? "true" : "false");

头文件。有问题的成员变量接近尾声,在 public: 标题下。

// 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();
};

【问题讨论】:

  • 不确定,但也许 DECLARE_MESSAGE_MAP() 宏会搞乱什么是公开的,什么是受保护的……在它之后添加 public: 然后再试一次。

标签: c++


【解决方案1】:

来自documentation of the DECLARE_MESSAGE_MAP()

注意

如果您在DECLARE_MESSAGE_MAP 之后声明任何成员,则必须为它们指定新的访问类型(publicprivateprotected)。

因此,宏扩展可能会导致可见性发生变化。将宏移动到类的末尾或在其后添加public:。我建议在您的代码中留下关于此的评论,以提醒您将来修改该类时这一事实。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2019-05-01
    • 1970-01-01
    相关资源
    最近更新 更多