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

Discussion in 'C++' started by 'Mani, Apr 3, 2006.

  1. 'Mani

    'Mani Guest

    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?
     
    'Mani, Apr 3, 2006
    #1
    1. Advertising

  2. 'Mani

    Phlip Guest

    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!!!
     
    Phlip, Apr 3, 2006
    #2
    1. Advertising

  3. 'Mani

    'Mani Guest

    Thanks Philip. Any idea why does the error come when i include it in
    the header file, but not in the cpp file?
     
    'Mani, Apr 3, 2006
    #3
  4. 'Mani

    Alan Johnson Guest

    '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
     
    Alan Johnson, Apr 3, 2006
    #4
  5. 'Mani

    gangs Guest

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

    gangs.
     
    gangs, Apr 3, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page