By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Forward declaration vs. include header

P: n/a
Hi,
I have a C++ header file like this,

#include "MyClass1.h"

class MyClass2{
private:
MyClass1* c1;
};
In MyClass.h file, there is class declaration of class MyClass1.

But VC7 compiler complains MyClass1 is not a type.

I have to add a forward declaration like this to make compiler
happy,
#include "MyClass1.h"

class MyClass1;

class MyClass2{
private:
MyClass1* c1;
};
I am wondering why is that. "#include" should copy whatever in
MyClass1.h to this header file, and so this header file should have
detailed MyClass1 declararion, right?

Jun 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<li*****@hotmail.com> wrote:
Hi,
I have a C++ header file like this,

#include "MyClass1.h"

class MyClass2{
private:
MyClass1* c1;
}; In MyClass.h file, there is class declaration of class MyClass1. But VC7 compiler complains MyClass1 is not a type. I have to add a forward declaration like this to make compiler
happy, #include "MyClass1.h"
Are there two files, MyClass.h and MyClass1.h ?

S.class MyClass1;

class MyClass2{
private:
MyClass1* c1;
};
I am wondering why is that. "#include" should copy whatever in
MyClass1.h to this header file, and so this header file should have
detailed MyClass1 declararion, right?

Jun 15 '06 #2

P: n/a
In article <11**********************@i40g2000cwc.googlegroups .com>,
li*****@hotmail.com wrote:
Hi,
I have a C++ header file like this,

#include "MyClass1.h"

class MyClass2{
private:
MyClass1* c1;
};
In MyClass.h file, there is class declaration of class MyClass1.

But VC7 compiler complains MyClass1 is not a type.

I have to add a forward declaration like this to make compiler
happy,
#include "MyClass1.h"

class MyClass1;

class MyClass2{
private:
MyClass1* c1;
};


My guess is that something is wrong in the "MyClass1.h" file. Maybe you
got your include guards messed up somehow.
Jun 15 '06 #3

P: n/a

Daniel T. wrote:
In article <11**********************@i40g2000cwc.googlegroups .com>,
li*****@hotmail.com wrote:
Hi,
I have a C++ header file like this,

#include "MyClass1.h"

class MyClass2{
private:
MyClass1* c1;
};
In MyClass.h file, there is class declaration of class MyClass1.

But VC7 compiler complains MyClass1 is not a type.

I have to add a forward declaration like this to make compiler
happy,
#include "MyClass1.h"

class MyClass1;

class MyClass2{
private:
MyClass1* c1;
};


My guess is that something is wrong in the "MyClass1.h" file. Maybe you
got your include guards messed up somehow.


I think you are right, the problem is include guards. I clean up the
guards and the problem is gone.

Thanks.

Jun 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.