Namespace across files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cole.harvey@yahoo.com

    #1

    Namespace across files

    I thought namespaces could be used globally across files. For example:

    File1.h

    namespace foo {
    typedef int test_type;
    } // Note this not in any class just defined in File1


    File2.h

    #include "File1.h"

    class bar {

    foo::test_type m_data;

    }

    The complier says: foo is not declared in this scope. What is up?

    Thanks

  • Amal P

    #2
    Re: Namespace across files

    Hi,
    namespace can be used accross files. I am not able to get any
    compile time error while compiling the code,

    /*-------FooNameSpace.h--------*/
    namespace foo
    {
    typedef int test_type;
    }

    /*-------UseFoo.h--------*/
    #include "FooNameSpace.h "
    class bar
    {
    foo::test_type data;
    };

    Hope this is same as the code what you have specified except there is a
    ' ; ' in this code after the class... I am using vc++ 6.0 compiler to
    compile my c++ code. Namespaces can surly be used accross files...
    There is no doubt in that. You may be doing somthing else wrong. Please
    try above code in your compiler and post the result in group... Are you
    sure that the file is geting included correctly?

    Best Regards
    Amal P.

    Comment

    • cole.harvey@yahoo.com

      #3
      Re: Namespace across files

      Okay this code worked. The problem I had was a circular dependency.
      So I had something like:

      /*-------FooNameSpace.h--------*/
      #include "UseFoo.h" // No good

      namespace foo
      {
      typedef int test_type;



      }


      /*-------UseFoo.h--------*/
      #include "FooNameSpace.h "
      class bar
      {
      foo::test_type data;


      };

      So you can define the namespace before the include or use class forward
      declarations, or? Thanks for the help.

      Comment

      Working...