473,396 Members | 1,683 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,396 software developers and data experts.

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

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
4 6936
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
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
'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
Can you give some more info on, Where have you declared the namespace
Utils?

gangs.

Apr 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Steven T. Hatton | last post by:
I recently came across the suggestion that it might be beneficial to create a header file containing all the Standard Headers, and include that in all the places where declarations from the...
2
by: trying_to_learn | last post by:
im in the primary stages of learning C++. The book im learning from says Dont use using namespace.. directive in header file However im trying to make the following header file.I need to include...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
11
by: Gary Wessle | last post by:
Hi is it right to have a line like #include <path/to/header.hfor a library on my system, in my header file and use some functions provided by this library in the implementation file (file.cpp)...
0
by: SamLee | last post by:
* environment : Linux UDB V8.2 1. a.h #define csize 13 struct { short len; char data; } firstnmest; 2. a.sqc #include <stdio.h> EXEC SQL INCLUDE SQLCA; int main() {
14
by: Jess | last post by:
Hello, I was told that if I have a template class or template function, then the definitions must be put into the header file where I put the declarations. On the other hand, it is generally...
1
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
by: nguillot | last post by:
Yes, by redeclaring the tB typedef in A.h: I really would prefer not to duplicate the typedef, for code maintenance.
2
johny10151981
by: johny10151981 | last post by:
Hello everybody, I am having a problem with including header file. The problem is stated below: file a.h struct __solid { char s; }solid;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.