473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Forward declaration vs. include header

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
3 6888
<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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
1
by: qazmlp | last post by:
Is it a good style to re-forward declare the types, which were already forward declared in base header file, in derived class header file? // base.h class addrClass; class base { public:...
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
5
by: Simon Elliott | last post by:
For some time I've been using typedefs for STL containers for reasons outlined in: http://www.gotw.ca/gotw/046.htm However a major downside to this is that you can't forward declare a typedef...
4
by: yuliy | last post by:
Hello gurus, I stuck in following: how can I do forward declaration if the forward declared class is in some namespace? something like // header class std::string; // approach#1
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
1
by: toton | last post by:
Hi, I have two namespace contains class InkFrame and PrefDialog respectively. PrefDialog needs InkFrame to show the dialog over the frame. It also stores a pointer to InkFrame inside it. Now I...
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.