I get the following errors in my MFC Project: (Please note: the bolded lines matches the title of my Error List in Visual Studio 2022)
Severity Code Description Project File Line Suppression State
Error (active) E1086 the object has type qualifiers that are not compatible with the member function "CappApp::GetThisMessageMap" app C:\Projects\C++Projects\app\app.cpp 21
Error C2662 'const AFX_MSGMAP *CappApp::GetThisMessageMap(void)': cannot convert 'this' pointer from 'const CappApp' to 'CappApp &' app C:\Projects\C++Projects\app\app.cpp 21
Error C1189 #error: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] main C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\atlmfc\include\afx.h 24
The last Error comes from an Error Directive, which prints the error as followed by C1189 into my error list. I ignored this in the past (because the program still executed), but I am in additional trouble with error C1086 and C1189. I don't know how to handle this. Could you please help?
This is the code from my MFC project
BEGIN_MESSAGE_MAP(CappApp, CWinAppEx)
// Standard file based document commands
// ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
// ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
END_MESSAGE_MAP()
Please note: The lines ON_COMAND ... I commented out, because I am not using the MFC menu, but if I don't the following messages show:
Severity Code Description Project File Line Suppression State
Error C2248 'CWinApp::OnFileOpen': cannot access protected member declared in class 'CWinApp' app C:\Projects\C++Projects\app\app.cpp 24
Error C2248 'CWinApp::OnFileNew': cannot access protected member declared in class 'CWinApp' app C:\Projects\C++Projects\app\app.cpp 23
Also: in case I would comment out BEGIN_MESSAGE_MAP until and including END_MESSAGE_MAP, I would obtain these error messages:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: virtual struct AFX_MSGMAP const * __cdecl CappApp::GetMessageMap(void)const " (?GetMessageMap@CappApp@@UEBAPEBUAFX_MSGMAP@@XZ) app C:\Projects\C++Projects\app\app.obj 1
Error LNK1120 1 unresolved externals app C:\Projects\C++Projects\app\x64\Debug\app.exe 1
The following is the code I need fixed in order to go ahead with my efforts:
BEGIN_MESSAGE_MAP(CappApp, CWinAppEx)
// Standard file based document commands
//ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
//ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
END_MESSAGE_MAP
These macros are defined in winafx.h as follows:
#define DECLARE_MESSAGE_MAP() \
protected: \
static const AFX_MSGMAP* PASCAL GetThisMessageMap(); \
virtual const AFX_MSGMAP* GetMessageMap() const override; \
#define BEGIN_TEMPLATE_MESSAGE_MAP(theClass, type_name, baseClass) \
PTM_WARNING_DISABLE \
template < typename type_name > \
const AFX_MSGMAP* theClass< type_name >::GetMessageMap() const \
{ return GetThisMessageMap(); } \
template < typename type_name > \
const AFX_MSGMAP* PASCAL theClass< type_name >::GetThisMessageMap() \
{ \
typedef theClass< type_name > ThisClass; \
typedef baseClass TheBaseClass; \
__pragma(warning(push)) \
__pragma(warning(disable: 4640)) /* message maps can only be called by single threaded message pump */ \
static const AFX_MSGMAP_ENTRY _messageEntries[] = \
{
#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
PTM_WARNING_DISABLE \
const AFX_MSGMAP* theClass::GetMessageMap() const \
{ return GetThisMessageMap(); } \
const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() \
{ \
typedef theClass ThisClass; \
typedef baseClass TheBaseClass; \
__pragma(warning(push)) \
__pragma(warning(disable: 4640)) /* message maps can only be called by single threaded message pump */ \
static const AFX_MSGMAP_ENTRY _messageEntries[] = \
{
#define END_MESSAGE_MAP() \
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
}; \
__pragma(warning(pop)) \
static const AFX_MSGMAP messageMap = \
{ &TheBaseClass::GetThisMessageMap, &_messageEntries[0] }; \
return &messageMap; \
} \
PTM_WARNING_RESTORE
// This is the end of BEGIN/END_MESSAGE_MAP in AfxWin.h
// =====================================================
END_MESSAGE_MAP()
Here is the additional code, requested by Min Xin Yu:
enter code here
// lc.cpp : Defines the class behaviors for the application.
//
#include "pch.h"
#include "framework.h"
#include "afxwinappex.h"
#include "afxdialogex.h"
#include "app.h"
#include "MainFrm.h"
#include "appDoc.h"
#include "appView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CappApp
// const AFX_MSGMAP* PASCAL GetThisMessageMap();
BEGIN_MESSAGE_MAP(CappApp, CWinApp)
//ON_COMMAND(ID_APP_ABOUT, &CappApp::OnAppAbout)
//// Standard file based document commands
//ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
//ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
//// Standard print setup command
//ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
// CappApp construction
CappApp::CappApp() noexcept{
// support Restart Manager
m_dwRestartManagerSupportFlags =
AFX_RESTART_MANAGER_SUPPORT_ALL_ASPECTS;
#ifdef _MANAGED
// If the application is built using Common Language Runtime
// support (/clr):
// 1) This additional setting is needed for Restart Manager
// support to work properly.
// 2) In your project, you must add a reference to
// System.Windows.Forms in order to build.
System::Windows::Forms::Application::
SetUnhandledExceptionMode(System
::Windows::Forms::UnhandledExceptionMode::ThrowException);
#endif
// TODO: replace application ID string below with unique ID
//string;
// recommended ormat for string is
// CompanyName.ProductName.SubProduct.VersionInformation
SetAppID(_T("lc.AppID.NoVersion"));
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CappApp object
CappApp theApp;
// CappApp initialization
BOOL CappApp::InitInstance() {
// InitCommonControlsEx() is required on Windows XP if an
// application
// manifest specifies use of ComCtl32.dll version 6 or later to
// enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to
// use in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
// Initialize OLE libraries
if (!AfxOleInit()) {
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
EnableTaskbarInteraction(FALSE);
// AfxInitRichEdit2() is required to use RichEdit control
// AfxInitRichEdit2();
// Standard initialization
// If you are not using these features and wish to reduce the
// size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something
// appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(4); // Load standard INI file options
// (including MRU)
// Register the application's document templates. Document
// templates
// serve as the connection between documents, frame windows and
// views
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CappDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CappView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line. Will return
// FALSE if app was launched with /RegServer, /Register,
// /Unregserver or /Unregister.
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and
// update it
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
int CappApp::ExitInstance()
{
//TODO: handle additional resources you may have added
AfxOleTerm(FALSE);
return CWinApp::ExitInstance();
}
// CappApp message handlers
// CAboutDlg dialog used for App About
//class CAboutDlg : public CDialogEx
//{
//public:
// CAboutDlg() noexcept;
//
// // Dialog Data
//#ifdef AFX_DESIGN_TIME
// enum { IDD = IDD_ABOUTBOX };
//#endif
//
//protected:
// virtual void DoDataExchange(CDataExchange* pDX);
// DDX/DDV support
//
// // Implementation
//protected:
// DECLARE_MESSAGE_MAP()
//};
//
//CAboutDlg::CAboutDlg() noexcept : CDialogEx(IDD_ABOUTBOX)
//{
//}
//
//void CAboutDlg::DoDataExchange(CDataExchange* pDX)
//{
// CDialogEx::DoDataExchange(pDX);
//}
//
//BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
//END_MESSAGE_MAP()
//
//// App command to run the dialog
//void CappApp::OnAppAbout()
//{
// CAboutDlg aboutDlg;
// aboutDlg.DoModal();
//}
// CappApp message handlers
This was the .cpp file, if needed I also deliver the .h file. Thank you.