namespace and #include

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hua.watson@gmail.com

    namespace and #include

    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....
  • puzzlecracker

    #2
    Re: namespace and #include

    On Sep 24, 7:35 am, Hua.wat...@gmai l.com wrote:
    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:

    Comment

    • Barry

      #3
      Re: namespace and #include

      On Sep 24, 7:35 pm, Hua.wat...@gmai l.com wrote:
      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.

      --
      Best Regards
      Barry

      Comment

      • Barry

        #4
        Re: namespace and #include

        On Sep 24, 7:35 pm, Hua.wat...@gmai l.com wrote:
        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.

        --
        Best Regards
        Barry

        Comment

        • Hua.watson@gmail.com

          #5
          Re: namespace and #include

          On 9ÔÂ24ÈÕ, ÏÂÎç10ʱ20·Ö, Barry <dhb2...@gmail. com>wrote:
          On Sep 24, 7:35 pm, Hua.wat...@gmai l.com wrote:
          >
          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.
          >
          --
          Best Regards
          Barry
          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


          Comment

          Working...