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

M

'Mani

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

Phlip

Mani said:
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.
 
M

'Mani

Thanks Philip. Any idea why does the error come when i include it in
the header file, but not in the cpp file?
 
A

Alan Johnson

'Mani said:
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
 
G

gangs

Can you give some more info on, Where have you declared the namespace
Utils?

gangs.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top