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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 'Mani

    #1

    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?

  • Phlip

    #2
    Re: Difference between including a header file in .h and .cpp

    Mani wrote:
    [color=blue]
    > Class Utils::Calc;[/color]

    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!!!


    Comment

    • 'Mani

      #3
      Re: Difference between including a header file in .h and .cpp

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

      Comment

      • Alan Johnson

        #4
        Re: Difference between including a header file in .h and .cpp

        'Mani wrote:[color=blue]
        > 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'.[/color]

        Tried to compile what?
        [color=blue]
        >
        > But including the OtherClass.h in MyClass.cpp file compiles fine.
        >
        > Any Idea?
        >[/color]

        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

        Comment

        • gangs

          #5
          Re: Difference between including a header file in .h and .cpp

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

          gangs.

          Comment

          Working...