namspace qn

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

    #1

    namspace qn

    I am trying to learn namespaces. in header exercise.h i wrote namespace
    //excerpt from exercise.h
    namespace trynamespace
    {
    class simpleclass
    {
    private:
    int i;
    public:
    simpleclass(int init): i(init) {}
    void set(int arg) {i=arg;}
    void print(void) const {P(i);} //P is a #define
    };

    extern simpleclass simpleclassobj;
    extern void blah(void);
    }

    in a cpp file i use the global object simpleclassobj and global function
    void blah(void);

    the definitions of the fns are in a *different* cpp file shown below
    (Version1_Doesn t_Work)
    However the code compiles fine but doesnt link.linker cant find
    simpleclassobj and function blah. However when I use scope resolution
    instead of using directive then everything works fine.!!.pls explain why
    isnt the using directive fine? and why i had to use scope resolution to
    make it work? thanks a lot.

    //*Version1_Doesn t_Work* : implementation of global fn and definition of
    //simpleclassobj

    #include "exercise.h "
    using namespace std;
    using namespace trynamespace;
    simpleclass simpleclassobj( 11111);
    void blah(void)
    {
    cout<<"fn blah called"<<endl;
    }
    ------------------------------------------
    // Version2_Works : implementation of global fn and definition of
    //simpleclassobj

    #include "exercise.h "
    using namespace std;
    //using namespace trynamespace; //*No need to use * using directive
    trynamespace::s impleclass trynamespace::s impleclassobj(1 1111);
    void trynamespace::b lah(void)
    {
    cout<<"fn blah called"<<endl;
    }
  • John Harrison

    #2
    Re: namspace qn


    "trying_to_lear n" <nospam@no.no > wrote in message
    news:cmsr23$mdd $1@gist.usc.edu ...[color=blue]
    > I am trying to learn namespaces. in header exercise.h i wrote namespace
    > //excerpt from exercise.h
    > namespace trynamespace
    > {
    > class simpleclass
    > {
    > private:
    > int i;
    > public:
    > simpleclass(int init): i(init) {}
    > void set(int arg) {i=arg;}
    > void print(void) const {P(i);} //P is a #define
    > };
    >
    > extern simpleclass simpleclassobj;
    > extern void blah(void);
    > }
    >
    > in a cpp file i use the global object simpleclassobj and global function
    > void blah(void);
    >
    > the definitions of the fns are in a *different* cpp file shown below
    > (Version1_Doesn t_Work)
    > However the code compiles fine but doesnt link.linker cant find
    > simpleclassobj and function blah. However when I use scope resolution
    > instead of using directive then everything works fine.!!.pls explain why
    > isnt the using directive fine? and why i had to use scope resolution to
    > make it work? thanks a lot.
    >
    > //*Version1_Doesn t_Work* : implementation of global fn and definition of
    > //simpleclassobj
    >
    > #include "exercise.h "
    > using namespace std;
    > using namespace trynamespace;
    > simpleclass simpleclassobj( 11111);
    > void blah(void)
    > {
    > cout<<"fn blah called"<<endl;
    > }
    > ------------------------------------------
    > // Version2_Works : implementation of global fn and definition of
    > //simpleclassobj
    >
    > #include "exercise.h "
    > using namespace std;
    > //using namespace trynamespace; //*No need to use * using directive
    > trynamespace::s impleclass trynamespace::s impleclassobj(1 1111);
    > void trynamespace::b lah(void)
    > {
    > cout<<"fn blah called"<<endl;
    > }[/color]

    You have to understand the difference between looking up a name in a
    namespace, and defining a name in a namespace.

    'using namespace trynamespace' only affects the way names are looked up,
    trynamespace gets added to the list of namespaces that will be searched for
    a name. There are two ways to define a name in a namespace, one is

    void trynamespace::b lah()
    {
    }

    the other is

    namespace trynamespace
    {
    void blah()
    {
    }
    }

    john


    Comment

    • angelo

      #3
      Re: namspace qn

      trying_to_learn wrote:[color=blue]
      > I am trying to learn namespaces. in header exercise.h i wrote namespace
      > //excerpt from exercise.h
      > namespace trynamespace
      > {
      > class simpleclass
      > {
      > private:
      > int i;
      > public:
      > simpleclass(int init): i(init) {}
      > void set(int arg) {i=arg;}
      > void print(void) const {P(i);} //P is a #define
      > };
      >
      > extern simpleclass simpleclassobj;
      > extern void blah(void);
      > }
      >
      > in a cpp file i use the global object simpleclassobj and global function
      > void blah(void);
      >
      > the definitions of the fns are in a *different* cpp file shown below
      > (Version1_Doesn t_Work)
      > However the code compiles fine but doesnt link.linker cant find
      > simpleclassobj and function blah. However when I use scope resolution
      > instead of using directive then everything works fine.!!.pls explain why
      > isnt the using directive fine? and why i had to use scope resolution to
      > make it work? thanks a lot.
      >
      > //*Version1_Doesn t_Work* : implementation of global fn and definition of
      > //simpleclassobj
      >
      > #include "exercise.h "
      > using namespace std;
      > using namespace trynamespace;
      > simpleclass simpleclassobj( 11111);
      > void blah(void)
      > {
      > cout<<"fn blah called"<<endl;
      > }
      > ------------------------------------------
      > // Version2_Works : implementation of global fn and definition of
      > //simpleclassobj
      >
      > #include "exercise.h "
      > using namespace std;
      > //using namespace trynamespace; //*No need to use * using directive
      > trynamespace::s impleclass trynamespace::s impleclassobj(1 1111);
      > void trynamespace::b lah(void)
      > {
      > cout<<"fn blah called"<<endl;
      > }[/color]

      Which compiler are you using and what's its version? I think version 1
      is correct and tried with g++3.3.1, then everything is fine.
      You included "excersise. h" into the "exercise.c pp", so it's equal to
      copy and paste the definition of namaspace trynamespace at the beginning
      part of the .cpp file. and using namespace trynamespace will actually
      make everything in trynamespace visible in the global namespace. So
      actually your code equals to :
      ------------------------------------------------
      class simpleclass
      {
      private:
      int i;
      public:
      simpleclass(int init): i(init) {}
      void set(int arg) {i=arg;}
      void print(void) const {P(i);} //P is a #define
      };

      extern simpleclass simpleclassobj;
      extern void blah(void);

      using namespace std;
      simpleclass simpleclassobj( 11111);

      void blah(void)
      {
      cout<<"fn blah called"<<endl;
      }
      --------------------------------------------------
      I see no problem with that.

      Comment

      Working...