namespace problem

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

    #1

    namespace problem

    I have a namespace with a collection of related functions. In the cpp file
    for their implementations I have:

    #include ...

    using namespace n;

    float f(){...}

    void g(...)
    {
    f();
    }

    I get an error saying that 'f' is an ambiguous call to an overloaded
    function...

    If I change my cpp to the following, it works, :

    #include ...

    float n::f(){...}

    void n::g(...)
    {
    f();
    }

    Why does the latter work and not the former? Also, in the latter
    implementation of g, why don't I have to write

    void n::g(...)
    {
    n::f();
    }

    Thanks.


  • Victor Bazarov

    #2
    Re: namespace problem

    "vsgdp" <nospam@nospam. com> wrote...[color=blue]
    > I have a namespace with a collection of related functions. In the cpp[/color]
    file[color=blue]
    > for their implementations I have:
    >
    > #include ...
    >
    > using namespace n;
    >
    > float f(){...}
    >
    > void g(...)
    > {
    > f();
    > }
    >
    > I get an error saying that 'f' is an ambiguous call to an overloaded
    > function...[/color]

    Do you have another 'f' somewhere, in the global scope? Ambiguity
    can only exist if there is more than one to choose from.
    [color=blue]
    >
    > If I change my cpp to the following, it works, :
    >
    > #include ...
    >
    > float n::f(){...}
    >
    > void n::g(...)
    > {
    > f();
    > }
    >
    > Why does the latter work and not the former? Also, in the latter
    > implementation of g, why don't I have to write
    >
    > void n::g(...)
    > {
    > n::f();[/color]

    Once you're in a function 'g' (who's part of 'n' namespace), you don't
    have to specify it, 'n' is searched first, and 'n::f' is found.
    [color=blue]
    > }[/color]

    Victor


    Comment

    • John Carson

      #3
      Re: namespace problem

      "vsgdp" <nospam@nospam. com> wrote in message
      news:OhB0b.5718 $Qy4.2192@fed1r ead05[color=blue]
      > I have a namespace with a collection of related functions. In the
      > cpp file for their implementations I have:
      >
      > #include ...
      >
      > using namespace n;
      >
      > float f(){...}
      >
      > void g(...)
      > {
      > f();
      > }
      >
      > I get an error saying that 'f' is an ambiguous call to an overloaded
      > function...
      >
      > If I change my cpp to the following, it works, :
      >
      > #include ...
      >
      > float n::f(){...}
      >
      > void n::g(...)
      > {
      > f();
      > }
      >
      > Why does the latter work and not the former? Also, in the latter
      > implementation of g, why don't I have to write
      >
      > void n::g(...)
      > {
      > n::f();
      > }
      >
      > Thanks.[/color]


      I suggest you do it this way:

      namespace n
      {
      float f(){...}

      void g(...)
      {
      f();
      }
      }

      using namespace n;

      dumps all of the content from the n namespace into the global namespace. By
      contrast

      namespace n
      {
      // stuff
      }

      re-opens namespace n and allows you to make additions to it.


      --

      John Carson
      1. To reply to email address, remove donald
      2. Don't reply to email address (post here instead)


      Comment

      • Kevin Goodsell

        #4
        Re: namespace problem

        Victor Bazarov wrote:[color=blue]
        > "vsgdp" <nospam@nospam. com> wrote...
        >[color=green]
        >>
        >>#include ...
        >>
        >>using namespace n;
        >>
        >>float f(){...}
        >>
        >>void g(...)
        >>{
        >> f();
        >>}
        >>
        >>I get an error saying that 'f' is an ambiguous call to an overloaded
        >>function...[/color]
        >
        >
        > Do you have another 'f' somewhere, in the global scope? Ambiguity
        > can only exist if there is more than one to choose from.[/color]

        Isn't the 'f' above actually in the global scope? I figured the
        ambiguity was between the 'f' above (::f) and the 'f' that is declared
        in the header file as n::f, but for which no definition exists.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Rolf Magnus

          #5
          Re: namespace problem

          Victor Bazarov wrote:
          [color=blue]
          > "vsgdp" <nospam@nospam. com> wrote...[color=green]
          >> I have a namespace with a collection of related functions. In the
          >> cpp[/color]
          > file[color=green]
          >> for their implementations I have:
          >>
          >> #include ...
          >>
          >> using namespace n;
          >>
          >> float f(){...}
          >>
          >> void g(...)
          >> {
          >> f();
          >> }
          >>
          >> I get an error saying that 'f' is an ambiguous call to an overloaded
          >> function...[/color]
          >
          > Do you have another 'f' somewhere, in the global scope?[/color]

          I guess not, but he's defining one in his example. And since he put a
          "using namespace n;" at the top, the f declared in the header is also
          put in the global namespace.
          [color=blue]
          > Ambiguity can only exist if there is more than one to choose from.[/color]

          There are two to choose from.

          Comment

          • Agent Mulder

            #6
            Re: namespace problem

            vsgdp> I have a namespace...

            Maybe this little program gives you the clue:


            class Furry{};//comment this out and it compiles
            namespace Green
            {
            class Furry{};//OR comment this out and it compiles
            }
            using namespace Green;//OR comment this out and it compiles
            int main(int argc,char**argv )
            {
            Furry furry;//OR comment this out and it compiles
            return 0;
            }

            -X










            Comment

            Working...