How to get rid of linking error in mfc? - c++

During the proccess of linking the LNK2001 error happens:
LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall CChildView::GetRuntimeClass(void)const " (?GetRuntimeClass#CChildView##UBEPAUCRuntimeClass##XZ)
Why would this be?
Here is the relevant code in the header:
class CChildView :public CDialog
{
DECLARE_DYNAMIC(CChildView)
public:
CChildView();
~CChildView();
afx_msg void OnPaint();
afx_msg void OnLevelProf();
afx_msg void OnLevelAmat();
afx_msg void OnLevelBeg();
afx_msg void OnStepC();
void new_game();
//void CloseWindow();
BOOL PreCreateWindow(CREATESTRUCT& cs);
int end_analyze();
void ii();
unsigned long calculate(int id, int x, int y);
afx_msg void OnNewGame();
//void Invalidate();
afx_msg void OnX1010();
afx_msg void OnX1919();
afx_msg void OnX3030();
afx_msg void OnX5050();
afx_msg void OnX100100();
//MessageBoxW();
void resize_window();
afx_msg void OnLButtonDown(UINT, CPoint xy);
//void GetWindowRect(RECT);
//int MessageBoxW();
void OnStepH();
void set_chеcked_menu(unsigned int old_id, unsigned int new_id);
DECLARE_MESSAGE_MAP()
};
And the part of .cpp file:
//IMPLEMENT_DYNAMIC(CChildView, CWnd)//!without this - doesn`t compiles. With - //runtime failure
BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
.....
END_MESSAGE_MAP()
But during the execution of my programm (if implement_dynamicaly is uncommented) it fails in AfxWinMain function on the line:
if (!pThread->InitInstance())
My other classes don't explicitly define them and they don't have errors.
Here is somethink like this, but it didn`t help me.
MFC dlg class link errors for MyClass::GetMessageMap() and MyClass::GetRuntimeClass (MSVC 2008)

You commented out the line IMPLEMENT_DYNAMIC(CChildView, CWnd).
You need to either comment out the DECLARE_DYNAMIC() macro in your CChildView class, or uncomment out the IMPLEMENT_DYNAMIC--they have to do with the CRuntimeClass for your class. Also, if you uncomment out the IMPLEMENT_DYNAMIC, you should make sure the baseclass in the macro matches the class you are deriving from. IOW, it should say CDialog and not CWnd. Also, your BEGIN_MESSAGE_MAP() suffers from the same problem.

Related

c++ Public member variable is inaccessible

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.

IN MFC CLISTCTRL a column should have an EDIT feature?

I am a beginner to MFC and C++ I want to have an edit option in one column I tried to overload these functions in the Classes but the Edit Option is not working could any one help me on this what is the mistake I did ?
class CEditableListCtrl : public CListCtrl
{
public:
int GetRowFromPoint( CPoint &point, int *col ) const;
CEdit* EditSubLabel( int nItem, int nCol );
void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
void OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult);
void OnLButtonDown(UINT nFlags, CPoint point);
};
class CInPlaceEdit : public CEdit
{
public:
CInPlaceEdit(int iItem, int iSubItem, CString sInitText);
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CInPlaceEdit)
public: virtual BOOL PreTranslateMessage(MSG* pMsg);
//}}AFX_VIRTUAL
public: virtual ~CInPlaceEdit();
// Generated message map functions
protected:
//{{AFX_MSG(CInPlaceEdit)
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnNcDestroy();
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
int m_iItem;
int m_iSubItem;
CString m_sInitText;
BOOL m_bESC;
};
If you only need one column to be editable, you don't need to do much: make sure your list has an LVS_EDITLABELS style. This will allow you to edit text in column 0. If you need to edit different column - change visible column order using CListCtrl::SetColumnOrderArray()

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

Unable to Fix a Linker Error in C++

I am working on a project which is very new to me. I have a very basic knowledge of C++ and I am just unable to fix a linker error that I am getting while trying to build the project in VC++. Following is the error and definitions of 2 classes involved.
1>Dlg_Container_View.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CPictureCtrl::~CPictureCtrl(void)" (??1CPictureCtrl##UAE#XZ) referenced in function __unwindfunclet$??0CDlg_Container_View##QAE#PAVCWnd###Z$0
1>Dlg_Container_View.obj : error LNK2019: unresolved external symbol "public: __thiscall CPictureCtrl::CPictureCtrl(void)" (??0CPictureCtrl##QAE#XZ) referenced in function "public: __thiscall CDlg_Container_View::CDlg_Container_View(class CWnd *)" (??0CDlg_Container_View##QAE#PAVCWnd###Z)
1>Dlg_Container_View.obj : error LNK2019: unresolved external symbol "public: int __thiscall CPictureCtrl::Load(class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > > &)" (?Load#CPictureCtrl##QAEHAAV?$CStringT#DV?$StrTraitMFC_DLL#DV?$ChTraitsCRT#D#ATL#####ATL###Z) referenced in function "protected: virtual int __thiscall CDlg_Container_View::OnInitDialog(void)" (?OnInitDialog#CDlg_Container_View##MAEHXZ)
1>.\Debug/EDiscovery.exe : fatal error LNK1120: 3 unresolved externals
The following are the class definitions (which I think are relevant. I have just copy pasted the complete defintion which might be irrelevant but I didnt want to miss out on anything). Sorry for this basic question.
class CPictureCtrl :
public CStatic
{
public:
//Constructor
CPictureCtrl(void);
//Destructor
~CPictureCtrl(void);
public:
//Loads an image from a file
BOOL LoadFromFile(CString &szFilePath);
//Loads an image from an IStream interface
BOOL LoadFromStream(IStream* piStream);
//Loads an image from a byte stream;
BOOL LoadFromStream(BYTE* pData, size_t nSize);
//Loads an image from a Resource
// BOOL LoadFromResource(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType);
//Overload - Single load function
BOOL Load(CString &szFilePath);
BOOL Load(IStream* piStream);
BOOL Load(BYTE* pData, size_t nSize);
// BOOL Load(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType);
//Frees the image data
void FreeData();
protected:
virtual void PreSubclassWindow();
//Draws the Control
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL OnEraseBkgnd(CDC* pDC);
private:
//Internal image stream buffer
IStream* m_pStream;
//Control flag if a pic is loaded
BOOL m_bIsPicLoaded;
//GDI Plus Token
ULONG_PTR m_gdiplusToken;
};
The above class is defined in the header file PictureCtrl.h which is included in the file where the following class is defined. Following is the class that has an object of above class as a member variable:
class CDlg_Container_View : public CDialog , public CParentIView
{
// Construction
public:
CDlg_Container_View(CWnd* pParent = NULL);
~CDlg_Container_View();
CDialog * GetDialog(const int idx);
void ClosePages();
virtual void SetCurSel(const int idx);
void AddPage(const char * cText, CDialog * pDlg, const UINT id);
CPtrArray pPages;
CStringArray csText;
CUIntArray csIds;// standard constructor
int iCurIdx;
CString csTitle;
virtual void Show_Dialog();
virtual void Hide_Dialog();
virtual void RegisterChildToParent(CString,CIView*);
virtual void ChangeBtnState(CString p_strBtnName,BOOL flag);
void SetControlText();
void ResetDialog();
void ShowMessageBox(CString msg);
void EnableProject(bool p_blFlag);
CFont m_StaticFont;
// virtual void fun();
// standard constructor
// Dialog Data
//{{AFX_DATA(CDlg_Container_View)
enum { IDD = IDD_DLG_CONTAINER_VIEW_DIALOG };
CRichEditCtrl m_REdit_DisplayProjectNots;
CRichEditCtrl m_REdit_AddProjectNotes;
CButton m_Grp_Bx_Project_Notes;
CButton m_btn_Report;
CButton m_btn_Export;
CButton m_btn_Error;
CStatic m_Lbl_Token_Balance;
CButton m_Btn_Add_Notes;
CStatic m_Lbl_Project;
CButton m_btn_Collect;
CComboBox m_Combo_Project_Name;
CStatic wndFrame;
CPictureCtrl m_picCtrl;
//CHyperLink m_hyper_Manage_Tokens;
CStatic m_hyper_Manage_Tokens;
CHyperLink m_WebIdHyperlink;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDlg_Container_View)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
CBrush m_brush;
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CDlg_Container_View)
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
virtual BOOL OnInitDialog();
// afx_msg void OnBtnCollect();
afx_msg void OnBtnReport();
afx_msg void OnBtnExport();
afx_msg void OnBtnClose();
// afx_msg void OnSelendokCOMBOProjectName();
afx_msg void OnBtnError();
afx_msg void OnBtnAddNotes();
afx_msg void OnEditchangeCOMBOProjectName();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
CDlg_Container_Presenter *m_objCDlg_Container_Presenter;
BOOL m_blFirstTime_Collect;
BOOL m_blEnableButtons;
CString m_strProjectNote;
bool m_blFirsttime_Project;
//bool m_bAutoComplete;
CString m_sTypedText;
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnEnSetfocusReditDisplayprojectnotes();
// afx_msg void OnCbnDropdownComboProjectName();
afx_msg void OnBnClickedCheck1();
afx_msg void OnBnClickedCheck2();
// afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
// afx_msg void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
// afx_msg void OnCbnSetfocusComboProjectName();
// afx_msg void OnCbnEditupdateComboProjectName();
afx_msg void OnCbnEditupdateComboProjectName();
afx_msg void OnCbnSelchangeComboProjectName();
//afx_msg void OnBnClickedBtnProjectEdit();
afx_msg void OnBnClickedBtnProjectEdit();
// afx_msg void OnCbnCloseupComboProjectName();
// afx_msg void OnCbnCloseupComboProjectName();
afx_msg void OnCbnCloseupComboProjectName();
afx_msg void OnBnClickedBtnCollect();
afx_msg void OnCbnSelendokComboProjectName();
CButton m_btn_Update_Project;
afx_msg void OnCbnDropdownComboProjectName();
afx_msg void OnBnClickedButton1();
afx_msg void OnStnClickedLblManageTokens();
void UpdateTokenCount();
CPictureCtrl m_LogoPic;
};
`
Those linker errors mean that you have not provided the definition of those three functions.
i.e constructor, destructor and Load function...
CPictureCtrl(void);
~CPictureCtrl(void);
BOOL Load(CString &szFilePath);
Did you include PictureCtrl.cpp in your project?

MFC dlg class link errors for MyClass::GetMessageMap() and MyClass::GetRuntimeClass (MSVC 2008)

I copied an existing header for a dlg box class (created with the dlg class wizard/mfc wizard). All seemed to go fine until I added the cpp file to the project. Now i get odd link errors for some mfc magic methods:
error LNK2001: unresolved external
symbol "public: virtual struct
CRuntimeClass * __thiscall
DlgGapWindow::GetRuntimeClass(void)const
"
(?GetRuntimeClass#DlgGapWindow##UBEPAUCRuntimeClass##XZ)
error LNK2001: unresolved external
symbol "protected: virtual struct
AFX_MSGMAP const * __thiscall
DlgGapWindow::GetMessageMap(void)const
"
(?GetMessageMap#DlgGapWindow##MBEPBUAFX_MSGMAP##XZ)
Why would this be?
Here is the relevant code in the header
class DlgGapWindow : public CDialog
{
DECLARE_DYNAMIC(DlgGapWindow)
public:
DlgGapWindow(CWnd* pParent = NULL);
virtual ~DlgGapWindow();
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual BOOL OnInitDialog();
enum { IDD = IDD_DIALOG_GAP_VIEW };// Dialog Data
GapViewer m_chart;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnSizing(UINT fwSide, LPRECT pRect) ;
afx_msg void OnTimer(ONTIMER_TYPE nIDEvent);
afx_msg void OnDestroy();
afx_msg void OnClose();
afx_msg void OnActivate(UINT,CWnd *,BOOL);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
I don't see anything from the class I modeled it after that seems to be missing. I have not found anything useful with google or other searches to indicate why these magic mfc things are missing. My other classes don't explicitly define them and they don't have errors.
The RC file does have a corresponding dlg definition.
EDIT:
Thanks for the DECLARE_DYNAMIC help - now I do not have the GetRuntimClass() error - just the GetMessagemap() error.
You used DECLARE_DYNAMIC but forgot IMPLEMENT_DYNAMIC.
Oops
I forgot BEGIN_MESSAGE_MAP.
Thanks for the help