Forward declaration vs. include header

L

linq936

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?
 
D

Daniel T.

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.
 
L

linq936

Daniel said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top