error C2309: is not a member of CDialog - c++

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.

Related

Syntax error in c++ default-int [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 4 years ago.
So I just began programming in c++ and I am planning to make a small game as my first project (with the SDL library).
So I have this really small piece of code in a header file that gives me an error that I can not solve.
main.h
#include "Screen.h"
#include "MainGame.h"
Screen* screen = nullptr; //main.h line 8
Screen.h
#pragma once
#include "SDL.h"
#include <string>
#include <stdio.h>
#include <iostream>
#include "GameState.h"
#include "Game.h"
using namespace std;
class Screen
{
public:
Screen(string name, int width, int height, GameState* state);
~Screen();
SDL_Window *window;
SDL_Surface *screen;
SDL_Renderer *renderer;
};
MainGame.h
#pragma once
#include "GameState.h"
#include <stdio.h>
class MainGame :
public GameState
{
public:
MainGame();
~MainGame();
void start();
void update();
void render();
void stop();
};
Game.h
#pragma once
#include "GameState.h"
#include "Screen.h"
#include "SDL.h"
#include "main.h"
class Game
{
public:
GameState* activestate;
Game(GameState state);
~Game();
void changeState(GameState newState);
bool isRunning;
void handleEvents();
void update();
void render();
void stop();
};
GameState.h
#pragma once
class GameState
{
public:
GameState();
~GameState();
virtual void start();
virtual void update();
virtual void stop();
virtual void render();
};
And it gives this errors:
Error C2143 syntax error: missing ';' before '* main.h 8
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int main.h 8
What do these errors mean and how do I solve them?
The error comes from your circular dependency. Some file includes Screen.h. Screen.h includes Game.h. Game.h includes main.h. main.h needs Screen.h but because of pragma once it can't include it. So it doesn't know class Screen. Either remove circular dependency (better solution if possible, here it's possible):
Remove #include "main.h" in Game.h
or use forward declaration:
Write class Screen; in main.h:7

Calling a method from another class in message handler

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

Can't use declared structure in another declaration

I have header that contains class declaration.
DialogExInput.h
#include "Stdafx.h"
#pragma once
#include "utils.h"
// CDialogExInput dialog
class CDialogExInput : public CDialogEx
{
DECLARE_DYNAMIC(CDialogExInput)
public:
CString timeRemainedSecStr;
CString Caption;
int TimeoutSec;
int MinDataSize;
int MaxDataSize;
CString text;
int timeRemainedSec;
void AssignParams (DialogOptions* dOp);
CDialogExInput(UINT nIDTemplate, CWnd *pParent = NULL);
afx_msg void OnTimer(UINT uTime);
virtual ~CDialogExInput();
void SetOnTop();
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
};
Compiler can't find DialogOptions, which is declared in utils.h. And DialogExInput.h includes utils.h.
Error 1 error C2061: syntax error : identifier 'DialogOptions'
utils.h
#ifndef UTILS_H
#define UTILS_H
#include "InputDialog.h"
#include <string>
using namespace std;
#define CURRENCY_EUR 978
extern HWND AuthMsgHWND;
struct DialogOptions {
CString Caption ;
CString DefaultText;
int MaxTimeoutExpected;
int MaxChars;
int MinChars;
int ModalResult;
int inputTimeRemained;
} ;
void ErrorExit();
const wchar_t *GetWC(const char *c);
bool GetProductAndVersion(CStringA & strProductName, CStringA & strProductVersion);
void doInputDialog(DialogOptions *pDo, string & answer);
void doYesNoDialog(DialogOptions *pDo, string & answer );
wstring termToWchar (const char* value);
string getTime(string format = "%d-%m-%Y %I:%M:%S" );
#endif UTILS_H
So where is problem? Why DialogExInput.h don't see DialogOptions
A circular include exists
DialogExInput.h
#pragma once
#include "utils.h"
InputDialog.h
#include "DialogExInput.h"
utils.h
#include "InputDialog.h"
So if you include InputDialog.h before any of the others (where it goes wrong) then the files will be read in the following order
Inputdialog.h
DialogExInput.h
utils.h
and DialogExInput can therefore not see the content of utils.h.
If you add the forward declaration of struct DialogOptions at the the start of DialogExInput .h
#include "Stdafx.h"
#pragma once
#include "utils.h"
struct DialogOptions; // forward declare as you only use a pointer/reference to it.
// CDialogExInput dialog
class CDialogExInput : public CDialogEx
Making a separate include file is also an option.
Also you should break the circular include, checking if you really need those includes. If you don't you it will get back and bite you later.

How can I fix missing ';' before '*' error in my program?

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

I can't seem to add a column header to a list box in an inherited MFC dialog. What's wrong?

I have a CStdDlg thats inherits from a CDialog class. In the CStdDlg dialog, I have a list box (m_lcList1), edit box (m_ceEdit1), a radio button(m_rbButton2) and buttons OK, Cancel and Button1.
I am trying to create another class named CDerivedDlg that inherits from CStdDlg. I want to use everything in CStdDlg but from the CDerivedDlg.
This is a silly test application here, but I need something exactly like this is a real application.
I'll show all the code below. PROBLEM: The problem keeps bombing whenever I try to add a column header to the list box. m_hWnd = NULL
Can anyone tell me what's wrong? I would truly appreciate it. Thank you.
// CStdDlg.cpp file
#include "stdafx.h"
#include "testdlg.h"
#include "StdDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog
CStdDlg::CStdDlg(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(CStdDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStdDlg)
//}}AFX_DATA_INIT
}
void CStdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStdDlg)
DDX_Control(pDX, IDC_EDIT1, m_ceEdit1);
DDX_Control(pDX, IDC_RADIO2, m_rbButton2);
DDX_Control(pDX, IDC_LIST1, m_lcList1);
//}}AFX_DATA_MAP
}
IMPLEMENT_DYNAMIC(CStdDlg, CDialog)
BEGIN_MESSAGE_MAP(CStdDlg, CDialog)
//{{AFX_MSG_MAP(CStdDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStdDlg message handlers
BOOL CStdDlg::OnInitDialog()
{
CDialog::OnInitDialog();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
===================================================================================
//CStdDlg.h file
#if !defined(AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// StdDlg.h : header file
//
#include <afxwin.h>
/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog
class CStdDlg : public CDialog
{
DECLARE_DYNAMIC(CStdDlg)
// Construction
public:
CStdDlg(UINT nIDTemplate,CWnd* pParent = NULL); // standard constructor
CString GetTitle() {
CString csTTL;
GetWindowText(csTTL);
return csTTL;
}
// Dialog Data
//{{AFX_DATA(CStdDlg)
enum { IDD = IDD_STDDLG };
CEdit m_ceEdit1;
CButton m_rbButton2;
CListCtrl m_lcList1;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CStdDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
void ShowMsg() { AfxMessageBox("ShowMsg from StdDlg"); }
// Generated message map functions
//{{AFX_MSG(CStdDlg)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
===================================================================================
//CDerivedDlg.cpp file
// StdDlg.cpp : implementation file
//
#include "stdafx.h"
#include "testdlg.h"
#include "CDerivedDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog
CDerivedDlg::CDerivedDlg(CWnd* pParent /*=NULL*/)
: CStdDlg(CDerivedDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDerivedDlg)
//}}AFX_DATA_INIT
}
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDerivedDlg)
//}}AFX_DATA_MAP
}
IMPLEMENT_DYNAMIC(CDerivedDlg, CStdDlg)
BEGIN_MESSAGE_MAP(CDerivedDlg, CStdDlg)
//{{AFX_MSG_MAP(CDerivedDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg message handlers
//void CDerivedDlg::OnOK()
//{
// // TODO: Add extra validation here
// AfxMessageBox("CDerived Class OK button pressed");
// CDialog::OnOK();
//}
BOOL CDerivedDlg::OnInitDialog()
{
CStdDlg::OnInitDialog();
SetWindowText("Derived Test Window");
m_lcList1.InsertColumn(0,"This is a test"); *******THIS IS WHERE IT CRASHES*****
m_rbButton2.ShowWindow(SW_HIDE);
m_ceEdit1.ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDerivedDlg::OnButton1()
{
// // TODO: Add extra validation here
AfxMessageBox("CDerived Button1 pressed");
AfxMessageBox(GetTitle());
}
void CDerivedDlg::OnOK()
{
AfxMessageBox("CDerived Class OK button pressed");
}
===================================================================================
// CDerivedDlg.h file
#if !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CDerivedDlg.h : header file
//
#include "StdDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog
class CDerivedDlg : public CStdDlg
{
DECLARE_DYNAMIC(CDerivedDlg)
// Construction
public:
CDerivedDlg(CWnd* pParent = NULL); // standard constructor
virtual void SetTTL(CString csTitle) { this->SetWindowText(csTitle); }
// Dialog Data
//{{AFX_DATA(CDerivedDlg)
enum { IDD = IDD_STDDLG };
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDerivedDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CDerivedDlg)
virtual BOOL OnInitDialog();
afx_msg void OnButton1();
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
===================================================================================
I'm guessing that since the DDX_Control() are on the base class, the derived class will not link the resource controls to their respective classes. You might want to try to change this:
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
into this:
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CStdDlg::DoDataExchange(pDX);
m_lcList1.InsertColumn(0,"This is a test"); *******THIS IS WHERE IT CRASHES*****
You have to create the control before you'll be able to use it.
See CListCtrl::Create and/or CListCtrl::CreateEx methods.
You must choose the LVS_REPORT style since you want to use the list as a report view control.