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

Difference between including a header file in .h and .cpp

P: n/a
Hi,

This is just a generic question, where i want to know what is the
difference in including a header file in a .h file and .cpp file.

I have a class called MyClass (MyClass.h & MyClass.cpp).
There is another class (OtherClass.h & OtherClass.cpp)
OtherClass.cpp has a forward declaration to a class called 'Calc' which
is in the namespace called 'Utils' like below:

Class Utils::Calc;

Now, i included the OtherClass.h file in my MyClass.h file and tried to
compile. I got the error:
something like: ' Calc is not a member of the namespace 'Utils'.

But including the OtherClass.h in MyClass.cpp file compiles fine.

Any Idea?

Apr 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Mani wrote:
Class Utils::Calc;


I don't know what that forward-declares. Prob'ly a class Utils containing a
nested class Calc.

You need this:

namespace Utils { class Calc; }

One of the points of namespaces is they never close. You can reopen and add
stuff to them at any time.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 3 '06 #2

P: n/a
Thanks Philip. Any idea why does the error come when i include it in
the header file, but not in the cpp file?

Apr 3 '06 #3

P: n/a
'Mani wrote:
Hi,

This is just a generic question, where i want to know what is the
difference in including a header file in a .h file and .cpp file.

I have a class called MyClass (MyClass.h & MyClass.cpp).
There is another class (OtherClass.h & OtherClass.cpp)
OtherClass.cpp has a forward declaration to a class called 'Calc' which
is in the namespace called 'Utils' like below:

Class Utils::Calc;

Now, i included the OtherClass.h file in my MyClass.h file and tried to
compile. I got the error:
something like: ' Calc is not a member of the namespace 'Utils'.
Tried to compile what?

But including the OtherClass.h in MyClass.cpp file compiles fine.

Any Idea?


It is hard to make any specific suggestions without seeing code. Can you
reduce your code to the smallest example that causes the error and post
that?

Alan
Apr 3 '06 #4

P: n/a
Can you give some more info on, Where have you declared the namespace
Utils?

gangs.

Apr 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.