c++ - 无法访问类 'CCustomCommandLineInfo' 中声明的私有(private)成员

标签 c++ mfc command-line-arguments

我正在尝试向现有 MFC 应用程序添加命令行界面,并在网上找到了一个类 at this website .我已经根据我的需要对其进行了调整,当我尝试构建时,我收到一条错误消息,内容为 "error C2248: 'CCustomCommandLineInfo::CCustomCommandLineInfo' : cannot access private member declared in class 'CCustomCommandLineInfo'" 这是我的代码:

class CCustomCommandLineInfo : public CCommandLineInfo
{
  CCustomCommandLineInfo()
  {
    //m_bExport = m_bOpen = m_bWhatever = FALSE;
    m_bNoGUI = m_baMode = FALSE;
  }

  // for convenience maintain 3 variables to indicate the param passed. 
  BOOL m_bNoGUI;            //for /nogui (No GUI; Command-line)
  BOOL m_baMode;            //for /adv (Advanced Mode)
 // BOOL m_bWhatever;       //for /whatever (3rd switch - for later date)

  //public methods for checking these.
public:
  BOOL NoGUI() { return m_bNoGUI; };
  BOOL aModeCmd() { return m_baMode; };
  //BOOL IsWhatever() { return m_bWhatever; };

  virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
  {
    if(0 == strcmp(pszParam, "/nogui"))
    {
      m_bNoGUI = TRUE;
    } 
    else if(0 == strcmp(pszParam, "/adv"))
    {
      m_baMode = TRUE;
    }
   // else if(0 == strcmp(pszParam, "/whatever"))
    // {
    //  m_bWhatever = TRUE;
    // }
  }
};

这是我的 InitInstance() 中的内容

// parse command line (cmdline.h)
CCustomCommandLineInfo oInfo;
ParseCommandLine(oInfo);
if(oInfo.NoGUI())
  {
    // Do something
  }
else if(oInfo.aModeCmd())
  {
    // Do whatever
  }

我该如何解决这个问题?

最佳答案

你有:

class CCustomCommandLineInfo : public CCommandLineInfo
{
  CCustomCommandLineInfo()
  {
    //m_bExport = m_bOpen = m_bWhatever = FALSE;
    m_bNoGUI = m_baMode = FALSE;
  }

这使得默认构造函数成为一个 private 函数。这就是为什么你不能使用:

CCustomCommandLineInfo oInfo;

将默认构造函数设为public

class CCustomCommandLineInfo : public CCommandLineInfo
{
  public:
  CCustomCommandLineInfo()
  {
    //m_bExport = m_bOpen = m_bWhatever = FALSE;
    m_bNoGUI = m_baMode = FALSE;
  }

关于c++ - 无法访问类 'CCustomCommandLineInfo' 中声明的私有(private)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30826141/

相关文章:

c++ - 从电子邮件正文调用 Win32 应用程序

C++11 lambda 作为默认函数参数

c++ - 将数据从 CSV 文件存储到 C++ 中的 MAP

c++ - 如何在C/C++中输出unicode字符

python - Python 中处理 sys 参数的规范方法是什么?

C++:如何切换参数?

c - 在 Visual Studio 2010 中传递命令行参数?

c++ - 如果你在成员函数中执行 "delete this;"会发生什么?

MFC 菜单项未正确调用模式对话框

visual-studio - MFC 不支持 WINVER 小于 0x0501