Re: organizing application properly question
Well yes i need to use the methods of CMainGui from simulation......for
reference i am writing a small application so it gives a clear idea about how
i want it to work....(please bear the variable name change...i have added
appropriate comments)
//SampleDlg.h file
#pragma once
#include "afxwin.h"
#include "Resource.h"
Class has the following additional method declarations
void addtoListBox(CString s);
//SampleDlg.cpp file
#include "stdafx.h"
#include "Sample.h"
#include "SampleDlg.h"
#include "Simulation.h"
simulation *sim; //global declaration
BOOL CSampleDlg::OnInitDialog()
{
sim = new sim(this);
}
void CSampleDlg::addtoListBox(CString s)
{
m_listbox.AddString(s); //Assume there is a listbox with variable
m_listbox
}
*********************************************
//simulation.h file
#include "SampleDlg.h"
class simulation
{
public:
CSampleDlg *pmain;
simulation(LPVOID wnd);
void interpret(LPVOID);
};
//simulation.cpp file
#include "stdafx.h"
#include "Simulation.h"
simulation::simulation(LPVOID wnd)
{
pmain = (CSampleDlg*)wnd;
}
void simulation::interpret(LPVOID wnd)
{
pmain->addtoListBox("Hello");
}
I hope this makes it more clear how it works right now......but i dont want
sim to be a global variable....i want to put it into CSampleDlg.h file as a
class member and then initialize it in InitDialog and use it in
simulation.cpp file??
Now how do i do the above following??
kunal
"Joseph M. Newcomer" wrote:
On Mon, 9 Apr 2007 17:36:03 -0700, kunal s patel <kunalspatel@discussions.microsoft.com>
wrote:
Hi all,
I am trying to built a mfc dialog based application but it keeps giving
class redefinition error. I am not able to understand how to organize my
application. Here are the relevant parts of the code
//MainGui.cpp
****
Where's stdafx.h?
****
#include "simulation.h"
//and other stuff and then i declare a global object sim
simulation sim;
//simulation.h
#include "MainGui.h"
*****
This seems an odd organization. You don't need to include MainGUI here.
You can just do a forward decl:
class CMainGui;
****
class simulation
{
CMainGui *pmain;
simulation(LPVOID wnd);
****
Is it an LPVOID or a CWnd *? Or a CMainGui *?
****
//rest other stuff
}
//simulation.cpp
#include "simulation.h"
simulation::simulation(LPVOID wnd)
{
pmain->(CMainGui*)wnd;
}
So Now here is my question......this piece of code works fine but i dont
want to declare sim as global object....i want to put it in CMainGui Class n
then initialize the object in InitDialog.....but now if i am adding it to
CMainGui Class....i need to add simulation.h in CMainGui.h which creates
problem of class redefinition.......what is the best way of organizing this
whole application
****
What you do here is get rid of the second #include of MainGui.h, declare it as a forward
decl, as indicated.
Generally, the object such as simulation should not know or care that the parent window is
a CMainGui*. A CWnd * should be sufficient, because nothing in simulation should be
calling methods of CMainGui, or using variables of CMainGui. If it is, there are more
serious organizational issues than conflicting declarations.
Note that all your header files should include the file
#pragma once
so you don't get duplicate definitions.
joe
*****
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm