Namespace across files

C

cole.harvey

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
 
A

Amal P

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.
 
C

cole.harvey

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.
 

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,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top