|
-
Sep 15th, 2007, 07:45 PM
#1
Thread Starter
Stuck in the 80s
Including Files
I'm trying to do something like this:
MainClass.h:
Code:
#ifndef __MAIN_CLASS_H
#define __MAIN_CLASS_H
#include "SecondaryClass.h"
class MainClass
{
private:
SecondaryClass *secondaryClass;
public:
MainClass();
~MainClass();
};
#endif
SecondaryClass.h:
Code:
#ifndef __SECONDARY_CLASS_H
#define __SECONDARY_CLASS_H
#include "MainClass.h"
class SecondaryClass
{
private:
MainClass *m_mainClass;
public:
SecondaryClass( MainClass* );
~SecondaryClass();
};
#endif
And I get the following errors:
In file included from MainClass.h:4,
from main.cpp:2:
SecondaryClass.h:9: error: ISO C++ forbids declaration of `MainClass' with no type
SecondaryClass.h:9: error: expected `;' before '*' token
make.exe: *** [main.o] Error 1
I'm sure this is because I am including a file that includes the file I'm currently in.
The point of this being that MainClass has a member variable of Secondary class, which also has a reference to the MainClass that instantiated it.
Any ideas on how to accomplish this?
-
Sep 15th, 2007, 08:03 PM
#2
Addicted Member
Re: Including Files
I think because never reashes to the end of the class...
See, is like this:
1) start header A.
2) header A calls header B.
3) start header B.
4) header B calls header A.
5) start header A.
6) header A calls header B, again!!!! why I didn't end the step 1 header A ?
...
Never reashes to the end of the class, because you are like inner loop or something the header over and over and over
-
Sep 15th, 2007, 08:04 PM
#3
Thread Starter
Stuck in the 80s
Re: Including Files
I don't think that would be the case due to the #ifndef #endif block around the code. The class is only defined once -- the second time, the code in the block should be ignored. Correct?
-
Sep 15th, 2007, 08:10 PM
#4
Addicted Member
Re: Including Files
Correct! But see this:
Even is "in used" the header, it never reashes the end of the header MainClass.
-
Sep 15th, 2007, 08:13 PM
#5
Thread Starter
Stuck in the 80s
Re: Including Files
I have no idea what you are talking about.
-
Sep 15th, 2007, 08:18 PM
#6
Addicted Member
Re: Including Files
And I don't why are you using this kinda code
-
Sep 15th, 2007, 08:19 PM
#7
Thread Starter
Stuck in the 80s
Re: Including Files
Is there something wrong with it?
-
Sep 15th, 2007, 08:26 PM
#8
Re: Including Files
I think what Kal-El means, is this:
C++ Code:
#ifndef __MAIN_CLASS_H
#define __MAIN_CLASS_H
// #include "SecondaryClass.h"
class MainClass
{
private:
SecondaryClass *secondaryClass;
public:
MainClass();
~MainClass();
};
#include "SecondaryClass.h" // Put it here, because the preprocessor will know what MainClass is now..
#endif
Although, I'm not sure if that is correct.. I'll test it soon and get back to you.
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 15th, 2007, 08:31 PM
#9
Thread Starter
Stuck in the 80s
Re: Including Files
Chemical, I do not believe I can do that, because MainClass needs to have the definition of SecondaryClass.
-
Sep 15th, 2007, 08:38 PM
#10
Re: Including Files
I've never tried something like this.. as I've never had the need to. What you might need, is a friend class to both of your current classes, which handles the information between the two.
I'll stuff around with your code for a bit and get back to you.
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 15th, 2007, 08:46 PM
#11
Re: Including Files
Actually.. silly me.
Just forward declare the classes:
MainClass.h
C++ Code:
#ifndef __MAIN_CLASS_H
#define __MAIN_CLASS_H
#include "SecondaryClass.h"
class SecondaryClass; // <------
class MainClass
{
private:
SecondaryClass *secondaryClass;
public:
MainClass();
~MainClass();
};
#endif
SecondaryClass.h
C++ Code:
#ifndef __SECONDARY_CLASS_H
#define __SECONDARY_CLASS_H
#include "MainClass.h"
class MainClass; // <--------
class SecondaryClass
{
private:
MainClass *m_mainClass;
public:
SecondaryClass( MainClass* );
~SecondaryClass();
};
#endif
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|