I' m creating a simple MFC text editor in VS2010 in order to learn C++ and I've hit a bit of a stumbling block....
Specifically, I get the following error in my build log:
1> Generating Code...
1>Link:
1> Creating library C:\Users\Alvin\Documents\Visual Studio 2010\Projects\Emergence\Debug\EmergenceHandlers.lib and object C:\Users\Alvin\Documents\Visual Studio 2010\Projects\Emergence\Debug\EmergenceHandlers.exp
1>EmergenceDoc.obj : error LNK2019: unresolved external symbol "public: __thiscall CEmergenceCntrItem::CEmergenceCntrItem(struct _reobject *,class CEmergenceDoc *)" (??0CEmergenceCntrItem##QAE#PAU_reobject##PAVCEmergenceDoc###Z) referenced in function "public: virtual class CRichEditCntrItem * __thiscall CEmergenceDoc::CreateClientItem(struct _reobject *)const " (?CreateClientItem#CEmergenceDoc##UBEPAVCRichEditCntrItem##PAU_reobject###Z)
1>C:\Users\Alvin\Documents\Visual Studio 2010\Projects\Emergence\Debug\EmergenceHandlers.dll : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:06.52
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
Below are all the files in which CEmergenceCntrItem appears....
cntritem.h:
#include "afxrich.h"
#include "Resource.h"
class CEmergenceDoc;
class CEmergenceView;
class CEmergenceCntrItem : public CRichEditCntrItem
{
DECLARE_SERIAL(CEmergenceCntrItem)
public:
CEmergenceCntrItem(REOBJECT* preo = NULL, CEmergenceDoc* pContainer = NULL);
public:
CEmergenceDoc* GetDocument()
{ return (CEmergenceDoc*)COleClientItem::GetDocument(); }
CEmergenceView* GetActiveView()
{ return (CEmergenceView*)COleClientItem::GetActiveView(); }
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWordPadCntrItem)
public:
protected:
//}}AFX_VIRTUAL
// Implementation
public:
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};
EmergenceView.cpp:
#include "afxrich.h"
#pragma once
class CEmergenceCntrItem;
class CEmergenceView : public CRichEditView
{
protected: // create from serialization only
CEmergenceView();
DECLARE_DYNCREATE(CEmergenceView)
public:
CEmergenceDoc* GetDocument() const;
public:
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);
public:
virtual ~CEmergenceView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
protected:
afx_msg void OnFilePrintPreview();
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnMutateGroup();
};
#ifndef _DEBUG // debug version in EmergenceView.cpp
inline CEmergenceDoc* CEmergenceView::GetDocument() const
{ return reinterpret_cast<CEmergenceDoc*>(m_pDocument); }
#endif
EmergenceDoc.cpp
#include "stdafx.h"
#include "Emergence.h"
#include "EmergenceDoc.h"
#include "CntrItem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CEmergenceDoc, CRichEditDoc)
BEGIN_MESSAGE_MAP(CEmergenceDoc, CRichEditDoc)
//{{AFX_MSG_MAP(CMyWordDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Enable default OLE container implementation
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS,
CRichEditDoc::OnUpdateEditLinksMenu)
ON_COMMAND(ID_OLE_EDIT_LINKS, CRichEditDoc::OnEditLinks)
ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST,
ID_OLE_VERB_LAST, CRichEditDoc::OnUpdateObjectVerbMenu)
END_MESSAGE_MAP()
CEmergenceDoc::CEmergenceDoc()
{
}
CEmergenceDoc::~CEmergenceDoc()
{
}
BOOL CEmergenceDoc::OnNewDocument()
{
if (!CRichEditDoc::OnNewDocument())
return FALSE;
return TRUE;
}
CRichEditCntrItem* CEmergenceDoc::CreateClientItem(REOBJECT* preo) const
{
return new CEmergenceCntrItem(preo, (CEmergenceDoc*) this);
}
void CEmergenceDoc::Serialize(CArchive& ar)
{
CRichEditDoc::Serialize(ar);
}
CntrItem.cpp
#include "stdafx.h"
#include "Emergence.h"
#include "EmergenceDoc.h"
#include "EmergenceView.h"
#include "cntritem.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_SERIAL(CEmergenceCntrItem, CRichEditCntrItem, 0)
CEmergenceCntrItem::CEmergenceCntrItem(REOBJECT *preo, CEmergenceDoc* pContainer)
: CRichEditCntrItem(preo, pContainer)
{
}
#ifdef _DEBUG
void CEmergenceCntrItem::AssertValid() const
{
CRichEditCntrItem::AssertValid();
}
void CEmergenceCntrItem::Dump(CDumpContext& dc) const
{
CRichEditCntrItem::Dump(dc);
}
#endif
Any help would be greatly appreciated.
Just copy cntritem.cpp and cntritem.h to resources folder!
Related
I have structure: PrintChooseDlg.h
#ifndef PRINTCHOOSEDLG_H
#define PRINTCHOOSEDLG_H
#include <string>
#pragma once
#endif
class CPrintChooseDlg : public CDialog
{
public:
int choosing;
/*afx_msg void OnPrinter1();
afx_msg void OnPrinter2();*/
CPrintChooseDlg(CWnd* pParent = NULL);
enum { IDD = IDD_PRINTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
//afx_msg void OnPrinter1();
//afx_msg void OnPrinter2();
virtual void OnPrinter1();
virtual void OnPrinter2();
DECLARE_MESSAGE_MAP()
};
and PrintChooseDlg.cpp
// PrintChoose.cpp : implementation file
//
#include "stdafx.h"
#include "Tungsten.h"
#include "PrintChooseDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// PrintChoose
//IMPLEMENT_DYNAMIC(PrintChoose, CWnd)
CPrintChooseDlg::CPrintChooseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPrintChooseDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChooseLabelDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CPrintChooseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChooseLabelDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPrintChooseDlg, CDialog)
ON_BN_CLICKED(IDC_PRINTER1,OnPrinter1)
ON_BN_CLICKED(IDC_PRINTER2,OnPrinter2)
END_MESSAGE_MAP()
// PrintChoose message handlers
void CPrintChooseDlg::OnPrinter1()
{
choosing=0;
CDialog::OnPrinter1();
}
void CPrintChooseDlg::OnPrinter2()
{
choosing=1;
CDialog::OnPrinter2();
}
and in the main file where i am running from, i define the following headers:
#include "stdafx.h"
#include "Tungsten.h"
#include "TungstenDlg.h"
using namespace std;
#include<sstream>
#include <string>
The problem is i am always getting the following errors: error C2039: 'OnPrinter1': is not a member of 'CDialog'
error C2039: 'OnPrinter2': is not a member of 'CDialog'
What i tried is to add #include <string> at my header and make sure that the headers are not repeated, and defining Printer1 and Pronter 2 in the main file where i am running from, but i still get the same error. I appreciate your help. Thanks in Advance
To get the address of a member function, you need to use &CPrintChooseDlg::OnPrinter1.
Really old VC++, like VC6, didn't care about the correct syntax and generated incorrect message maps.
I have .H file and .Cpp file for a dialog Print that contains two buttons Printer 1 and Printer 2 and I want to call a method in another class function when Printer 1 is pressed.
.H File
#pragma once
#ifndef PRINTCHOOSEDLG_H
#define PRINTCHOOSEDLG_H
class CPrintChooseDlg : public CTungstenDlg
{
public:
CPrintChooseDlg(CWnd* pParent = NULL);
enum { IDD = IDD_PRINTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
afx_msg void OnPrinter1();
afx_msg void OnPrinter2();
//virtual void OnPrinter1();
//virtual void OnPrinter2();
DECLARE_MESSAGE_MAP()
};
#endif
.Cpp
#include "stdafx.h"
#include "Tungsten.h"
#include "PrintChooseDlg.h"
CPrintChooseDlg::CPrintChooseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPrintChooseDlg::IDD, pParent)
{
}
void CPrintChooseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CPrintChooseDlg, CDialog)
ON_BN_CLICKED(IDC_PRINTER1,OnPrinter1)
ON_BN_CLICKED(IDC_PRINTER2,OnPrinter2)
END_MESSAGE_MAP()
void CPrintChooseDlg::OnPrinter1()
{
CTungstenDlg aux;
aux.OnOK();
}
void CPrintChooseDlg::OnPrinter2()
{
CTungstenDlg aux;
aux.OnOK();
}
How can i call the method CTungsten::OnOK inside the function CPrintChooseDlg::OnPrinter1()
It gives me an error of course because it is not defined in the PrintChooseDlg.cpp
What i tried: is to include guards in header and use suggested solutions in comments
Thanks in Advance
I have main executable and two functions that are dereferenced to DLL.
class CPluginInterface
{
public:
virtual void A(void) = 0;
virtual void B(void) = 0;
};
I created DLL like this
//Main.h
#include "Function.h"
class CForward : public CPluginInterface
{
public:
//void A(void);
void B(void);
};
//Main.cpp
#include "Main.h"
/*void CForward::A(void)
{
//A Function is commented because it is not used
}*/
void CForward::B(void)
{
//Do something here
}
extern "C"
{
// Plugin factory function
//void __declspec(dllexport) __cdecl A(void) { }
void __declspec(dllexport) __cdecl B(void) { }
}
However the program is crashed because A(void) doesn't exist when the main executable dereferencing it. How to skip A(void)?
If I create the DLL like this, it works fine.
//Main.h
#include "Function.h"
class CForward : public CPluginInterface
{
public:
void A(void);
void B(void);
};
//Main.cpp
#include "Main.h"
void CForward::A(void)
{
//Do something here
}
void CForward::B(void)
{
//Do something here
}
extern "C"
{
// Plugin factory function
void __declspec(dllexport) __cdecl A(void) { }
void __declspec(dllexport) __cdecl B(void) { }
}
NB: I create Plugin Interface.
The =0 suffix on your interface's virtual functions indicate that they are pure virtual and that you are required to override them when inheriting from the base class. In your first example CForward is an abstract class because you have not overridden A, thus you cannot create an instance of CForward.
https://stackoverflow.com/a/2652223
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?
I tried to make MyWindowSplitter class and I made the other new class derived from CView class as runtime class in MySplitter class. But when I tried to compile that I got these errors in MyProjectView.h in GetDocument function:
Error 1
error C2143: syntax error : missing ';' before '*'
Error 2
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Where is the problem and How can I fix them?
//MySplitter.cpp
#include "StdAfx.h"
#include "MySplitter.h"
#include "SplitDemoSixView.h"
#include "TestView.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CMySplitter::CMySplitter(void)
{
}
CMySplitter::~CMySplitter(void)
{
}
void CMySplitter::ChangeViewClass(CRuntimeClass* pNewView)
{
m_pDynamicViewClass = pNewView;
}
void CMySplitter::DeleteView(int row, int col)
{
CView* pView = (CView*)GetDlgItem(IdFromRowCol(row, col));
if(pView->IsKindOf(RUNTIME_CLASS(CSplitDemoSixView)))
{
ChangeViewClass(RUNTIME_CLASS(CSplitDemoSixView));
}
else
{
if(pView->IsKindOf(RUNTIME_CLASS(CTestView)))
{
ChangeViewClass(RUNTIME_CLASS(CTestView));
}
}
CSplitterWnd::DeleteView(row, col);
}
//TestView.cpp drived from CView
#include "stdafx.h"
#include "SplitDemoSix.h"
#include "TestView.h"
#include "SplitDemoSixDoc.h"
#include "SplitDemoSixView.h"
// CTestView
IMPLEMENT_DYNCREATE(CTestView, CView)
CTestView::CTestView()
{
}
CTestView::~CTestView()
{
}
BEGIN_MESSAGE_MAP(CTestView, CView)
END_MESSAGE_MAP()
// CTestView drawing
void CTestView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}
// CTestView diagnostics
#ifdef _DEBUG
void CTestView::AssertValid() const
{
CView::AssertValid();
}
#ifndef _WIN32_WCE
void CTestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif
#endif //_DEBUG
// CTestView message handlers
And the problem is here:
//MyProjectView.h
#pragma once
#include "resource.h"
#include "MySplitter.h"
class CSplitDemoSixView : public CFormView
{
protected: // create from serialization only
CSplitDemoSixView();
DECLARE_DYNCREATE(CSplitDemoSixView)
public:
enum{ IDD = IDD_SPLITDEMOSIX_FORM };
// Attributes
public:
//ERROR PERFORMS IN THIS FUNCTION:
CSplitDemoSixDoc* GetDocument();
// Operations
public:
// Overrides
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void OnInitialUpdate();
...
You need a forward declaration for your CDocument-derived class:
// Attributes
public:
//ERROR PERFORMS IN THIS FUNCTION:
class CSplitDemoSixDoc;
CSplitDemoSixDoc* GetDocument();