2 classes header file inclusion probleml !!
Dear Friends
I am finding difficulty this is a Qt error but I think the problem is C++ related.
I am getting these errors when I am including a header file (a class) whose object I want to access from some other class ..
These two classes
'meshparameterdlg.h' has the class 'MeshParameterDlg'
'controlPointsEditorDlg.h' has the class 'ControlPointsEditorDlg'
Both the classes are inherited by QDialog.
I am including 'controlPointsEditorDlg.h' in 'meshparameterdlg.h' and
'meshparameterdlg.h' in 'controlPointsEditorDlg.h' .
I want to have 'MeshParameterDlg' object as a data member of 'ControlPointsEditorDlg' and
'ControlPointsEditorDlg' object as a data member of 'MeshParameterDlg'.
when I am adding 'controlPointsEditorDlg.h' in 'meshparameterdlg.h' then there's no problem. When user presses a button from 'MeshParameterDlg' dialog then I am instantiating another dialog class that is 'ControlPointsEditorDlg' and this dialog appears and when user presses 'ok' button on 'ControlPointsEditorDlg' class then I should get some values from the QTextEdit from 'ControlPointsEditorDlg' class, so I want 'meshparameterdlg.h' to be included in 'controlPointsEditorDlg.h' so that I can put the value from the 2nd dialog to a member in the 1st dialog.
When I am adding 'meshparameterdlg.h' in 'controlPointsEditorDlg.h' I am getting these errors.
Please see below errors.
///////////////////////////////////////////////////////////////////////////////////////////////////////
In file included from src/../include/mainwindow.h:35,
from src/../include/graphicsscene.h:19,
from src/../include/graphicsview.h:19,
from src/../include/geometry.h:25,
from src/../include/checksmoothness.h:26,
from src/checksmoothness.cpp:14:
src/../include/meshparameterdlg.h:11: error: redefinition of `class MeshParameterDlg'
src/../include/meshparameterdlg.h:11: error: previous definition of `class MeshParameterDlg'
///////////////////////////////////////////////////////////////////////////////////////////////////////
But I am not doing anythin in 'meshparameterdlg.h' at line #11. But I am adding meshparameterdlg.h header in a separate class called 'controlPointsEditorDlg.h'
in both the header file i am adding
#ifndef HEADER_H
#define HEADER_H
class definition
#endif
Could you give me some solution to this problem I am fed up for the past 2 days with these.
Re: 2 classes header file inclusion probleml !!
Quote:
Originally Posted by
sujan.dasmahapatra
I want to have 'MeshParameterDlg' object as a data member of 'ControlPointsEditorDlg' and
'ControlPointsEditorDlg' object as a data member of 'MeshParameterDlg'.
You can't do that.
You have to forward declare one of that classes and use a pointer or a reference instead of a member object.
Kurt
Re: 2 classes header file inclusion probleml !!
Quote:
Originally Posted by
ZuK
You can't do that.
You have to forward declare one of that classes and use a pointer or a reference instead of a member object.
Kurt
IE.
in the ControlPointsEditorDlg.h file
Code:
class CMeshParameterDlg;
class CControlPointsEditorDlg : public CDialog
{
....yadda yadda
CMesgParameterDlg *m_dlg;
}
Then include MeshParameterDlg.h in the ControlPointsEditorDlg.cpp file.
Re: 2 classes header file inclusion probleml !!
Ask someone to put two boxes inside each other, and they'll tell you it's impossible.
And yet it seems like people are always trying to do the equivalent in code....
Re: 2 classes header file inclusion probleml !!
Quote:
Originally Posted by Lindley
Ask someone to put two boxes inside each other, and they'll tell you it's impossible.
unless they are quantum boxes :D !
Re: 2 classes header file inclusion probleml !!
Will C++0x support the quantum keyword?
Re: 2 classes header file inclusion probleml !!
uhm... just dreaming...
Code:
int main()
{
int a = 0;
quantum int* b = &a;
quantum int* b = 0;
*b = 2; // this is (|defined> + |undefined>)/sqrt(2) behaviour
}
( ok, I'll stop diverting this thread... :) )
Re: 2 classes header file inclusion probleml !!
Even if we forward decalre the class and declare a pointer for that we need to include the header file right !
When I am inclusing 2nd header class file in the 1st header class file then there's no problem but when the moment I declare the 1st header class file in the 2nd header class file these errors come.
I'll put the two header files tomorrow see if you could help me !
Thanks
Re: 2 classes header file inclusion probleml !!
no, you do not need to include the header file for class 1 in the header file for class 2 if class 1 is forward declared in the header for class 2 and only pointers or references to class 1 are used in the header for class 2. That is why it breaks the circular dependency.
Re: 2 classes header file inclusion probleml !!
The key is that you not try to do anything *with* those pointers or references in the header file except state that they exist.
All actual operations on those pointers/references should be placed in the source file, which *can* safely include the relevant header.
Re: 2 classes header file inclusion probleml !!
//1st header file -- meshparameterdlg.h
////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef MEHSPARAMETERDLG_H
#define MESHPARAMETERDLG_H
#include <QDialog>
#include "meshInputParameter.h"
#include "controlPointsEditorDlg.h" // This is not a problem
#include "controlPointsEditorNormalDistributionDlg.h"
class ControlPointsEditorDlg;
class ControlPointsEditorNormalDistributionDlg;
class MeshParameterDlg:public QDialog
{
Q_OBJECT
public:
ControlPointsEditorNormalDistributionDlg *controlPointsNormamlDistributionDlg;
ControlPointsEditorDlg *controlPointsDlg;
bool distributionFlag;
int i;
MeshParameterDlg();
~MeshParameterDlg();
Ui::Dialog ui;
private slots:
void load_gcf();
void save_gcf();
void o_mesh_radioOn();
void c_mesh_radioOn();
void surface_distribution();
void surface_distribution_new();
void normal_distribution();
void normal_distribution_new();
void tanh();
void sinh();
void use_text_editor();
void restore_default();
void ok();
private:
//////////////////////////////////////////////////////////////////////////////
QString para1, para2, para3, para4, para5, para6, para7, para8, para9, para10;
//////////////////////////////////////////////////////////////////////////////
QString meshtype;
int meshpoints_block1, meshpoints_block2;
double domain_height, first_cell_height;
int surface_distribution_noOfPoints;
double *surface_distribution_points;
int normal_distribution_noOfPoints;
double *normal_distribution_points;
QString stretch_function;
double volume_blend, dissipation_factor;
int omesh_outlet_block1, omesh_outlet_block2;
double wake_contraction;
};
#endif //MESHPARAMETERDLG_H
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//2nd header file -- controlPointsEditorDlg.h
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef CONTROLPOINTSEDITORDLG_H
#define CONTROLPOINTSEDITORDLG_H
#include <QDialog>
#include "controlPointsEditor.h"
#include "meshparameterdlg.h" // This is the problem
class ControlPointsEditorDlg:public QDialog
{
Q_OBJECT
public:
ControlPointsEditorDlg();
~ControlPointsEditorDlg();
Ui::Dialog1 ui;
int i;
bool flag;
int noOfPoints;
double *points;
private slots:
void clear();
void ok();
};
#endif //CONTROLPOINTSEDITORDLG_H
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// errors --
///////////////////////////////////////////
In file included from src/../include/mainwindow.h:35,
from src/../include/graphicsscene.h:19,
from src/../include/graphicsview.h:19,
from src/../include/geometry.h:25,
from src/../include/checksmoothness.h:26,
from src/checksmoothness.cpp:14:
src/../include/meshparameterdlg.h:11: error: redefinition of `class MeshParameterDlg'
src/../include/meshparameterdlg.h:11: error: previous definition of `class MeshParameterDlg'
src/checksmoothness.cpp: In member function `void CheckSmoothness::ok()':
src/checksmoothness.cpp:389: warning: unused variable 'n'
make: *** [obj/checksmoothness.o] Error 1
////////////////////////////////////////////////////////////////////////////////////////////////////////////