Doubt about namespaces

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

    #1

    Doubt about namespaces

    //general.h
    #ifndef _GENERAL_H
    #define _GENERAL_H

    namespace ip {

    bool esServicioIP();

    }

    #endif //_GENERAL_H

    //general.cc
    #include <general.h>

    ....

    using namespace ip;

    bool esServicioIP() {
    ....
    }

    //distsgip.cc

    .....

    if(esServicioIP ()) {
    ...
    }


    Code compiles ok, but linker gives me an error saying ip::esServicioI P
    is undefined.
    Instead if I put namespace ip when defining esServicioIP:

    bool ip::esServicioI P() {
    ....
    }
    Code compiles and links ok.

    In many other pieces of code with clases instead of functions inside a
    namespace, I don't need to qualify class' methods with namespace:

    bool MyClass::myMeth od() {
    ....
    }

    Compiles and links ok

    Is there any rule (perhaps of naming resolution or something else)
    different for classes and normal functions?

    Thanks in advance
  • David Harmon

    #2
    Re: Doubt about namespaces

    On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez
    <cmg250@nospam. tid.eswrote,
    >using namespace ip;
    >
    >bool esServicioIP() {
    >...
    >}
    That function is not "inside" the ip namespace. It is a new
    definition of its own independent function.
    >In many other pieces of code with clases instead of functions inside a
    >namespace, I don't need to qualify class' methods with namespace:
    >
    >bool MyClass::myMeth od() {
    >...
    >}
    That function must be a member of some namespace or class known as
    MyClass that already exists. It's not a valid declaration of
    MyClass. Furthermore, MyClass::myMeth od must have already been
    declared inside the namespace or class.
    >Is there any rule (perhaps of naming resolution or something else)
    >different for classes and normal functions?
    Not really. For example,

    using namespace ip;
    MyClass foo; // has no connection with any ip::foo
    class bar; // has no connection with any ip::bar

    Comment

    • Carlos Martinez

      #3
      Re: Doubt about namespaces

      David Harmon wrote:
      On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez
      <cmg250@nospam. tid.eswrote,
      >
      >using namespace ip;
      >>
      >bool esServicioIP() {
      >...
      >}
      >
      That function is not "inside" the ip namespace. It is a new
      definition of its own independent function.
      Excuse me. I have ommited code in .h because I have considered obvious.

      In .h

      namespace ip {

      bool esServicioIP();

      }
      >
      >In many other pieces of code with clases instead of functions inside a
      >namespace, I don't need to qualify class' methods with namespace:
      >>
      >bool MyClass::myMeth od() {
      >...
      >}
      In .h

      namespace ip {

      class MyClass {
      bool myMethod();
      }

      }
      >
      That function must be a member of some namespace or class known as
      MyClass that already exists. It's not a valid declaration of
      MyClass. Furthermore, MyClass::myMeth od must have already been
      declared inside the namespace or class.
      >
      >Is there any rule (perhaps of naming resolution or something else)
      >different for classes and normal functions?
      >
      Not really. For example,
      >
      using namespace ip;
      MyClass foo; // has no connection with any ip::foo
      class bar; // has no connection with any ip::bar
      >

      Comment

      • David Harmon

        #4
        Re: Doubt about namespaces

        On Tue, 12 Sep 2006 13:36:31 +0200 in comp.lang.c++, Carlos Martinez
        <cmg250@nospam. tid.eswrote,
        >David Harmon wrote:
        >On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez
        ><cmg250@nospam .tid.eswrote,
        >>
        >>using namespace ip;
        >>>
        >>bool esServicioIP() {
        >>...
        >>}
        >>
        >That function is not "inside" the ip namespace. It is a new
        >definition of its own independent function.
        >
        >Excuse me. I have ommited code in .h because I have considered obvious.
        >
        >In .h
        >
        >namespace ip {
        >
        bool esServicioIP();
        >
        >}
        You are quite right, it is obvious enough. You have two functions.

        In .h you declare ip::esServicioI P().
        in .cc you define esServicioIP().

        There is no reason you cannot have both of them. The compiler can
        only hope you define ip::esServicioI P() somewhere too.

        Comment

        • F.J.K.

          #5
          Re: Doubt about namespaces

          Carlos Martinez wrote:
          Is there any rule (perhaps of naming resolution or something else)
          different for classes and normal functions?
          >
          Thanks in advance
          No. Try harder to get a minimum testcase, as required by the FAQ and
          you'll see.

          Comment

          Working...