namespace and #include

H

Hua.watson

I want to add some declaration intio my namespace. But I do not have
the right to modify proto header files.
So I try


namespace mynamespace{
#include "a.h"
#include "b.h"
}

This is not a good style. But it even cannot work..... functions in
a.h has NOT been put into mynamespace....
 
P

puzzlecracker

I want to add some declaration intio my namespace. But I do not have
the right to modify proto header files.
So I try

namespace mynamespace{
#include "a.h"
#include "b.h"

}

This is not a good style. But it even cannot work..... functions in
a.h has NOT been put into mynamespace....

This should help you solve your problems with migrating to namespace:
http://www.gotw.ca/gotw/053.htm
 
B

Barry

I want to add some declaration intio my namespace. But I do not have
the right to modify proto header files.
So I try

namespace mynamespace{
#include "a.h"
#include "b.h"

}

This is not a good style. But it even cannot work..... functions in
a.h has NOT been put into mynamespace....

you also need to put the definition into your new namespace.

I guess you forgot to wrap your cxx files with your namespace.
suppose you have a.cpp corresponding to a.h, whereas b.cpp for a.h

use wrappers like a_wrap.cpp as following, and do remember to have
header
inclusion guard.

suppose A_H_INCLUDED for a.h

#include "a.h" // A_H_INCLUDED is define here

namespace mynamespace {
#include "a.cpp" // preprocesser #include "a.h" in a.cpp will be
discarded
}

exclude a.cpp in your project. while a_wrap.cpp is included

HTH.
 
B

Barry

I want to add some declaration intio my namespace. But I do not have
the right to modify proto header files.
So I try

namespace mynamespace{
#include "a.h"
#include "b.h"

}

This is not a good style. But it even cannot work..... functions in
a.h has NOT been put into mynamespace....

you need to put the definition into your namespace

I guess you forgot to wrap your cxx files into you namespace
suppose you have a.cpp for a.h

remember to have header inclusion guard
suppose A_H_INCLUDED for a.h

so write your a_wrap.cpp like this:

#include "a.h" // A_H_INCLUDED is defined

namespace mynamespace {
#include "a.cpp" // a.h in a.cpp will be discarded
}


HTH.
 
H

Hua.watson

you need to put the definition into your namespace

I guess you forgot to wrap your cxx files into you namespace
suppose you have a.cpp for a.h

remember to have header inclusion guard
suppose A_H_INCLUDED for a.h

so write your a_wrap.cpp like this:

#include "a.h" // A_H_INCLUDED is defined

namespace mynamespace {
#include "a.cpp" // a.h in a.cpp will be discarded

}

HTH.
Thanks, my code is like:

In global.h:
typedef int int32_t;
enum{ENUM_A};

In someone.h:
enum{ENUM_A};

In my.h:
namespace myspace{
#include "a.h"
}

In my.cpp
#include "global.h"
#include "someone.h"

myspace::int32_t ........

Then compiler tell me 2 error
1. ENUM_A conflict
2. int32_t is not in namespace myspace

In fact, I need include both global.h and someone.h to use some
functions, however, they defined some same unnamed enum. Now I have to
use 2 cpp file.....

Thanks
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top