namespace problem

Discussion in 'C++' started by vsgdp, Aug 20, 2003.

  1. vsgdp

    vsgdp Guest

    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.
     
    vsgdp, Aug 20, 2003
    #1
    1. Advertising

  2. "vsgdp" <> wrote...
    > 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...


    Do you have another 'f' somewhere, in the global scope? Ambiguity
    can only exist if there is more than one to choose from.

    >
    > 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();


    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.

    > }


    Victor
     
    Victor Bazarov, Aug 20, 2003
    #2
    1. Advertising

  3. vsgdp

    John Carson Guest

    "vsgdp" <> wrote in message
    news:OhB0b.5718$Qy4.2192@fed1read05
    > 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.



    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)
     
    John Carson, Aug 20, 2003
    #3
  4. Victor Bazarov wrote:
    > "vsgdp" <> wrote...
    >
    >>
    >>#include ...
    >>
    >>using namespace n;
    >>
    >>float f(){...}
    >>
    >>void g(...)
    >>{
    >> f();
    >>}
    >>
    >>I get an error saying that 'f' is an ambiguous call to an overloaded
    >>function...

    >
    >
    > Do you have another 'f' somewhere, in the global scope? Ambiguity
    > can only exist if there is more than one to choose from.


    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.
     
    Kevin Goodsell, Aug 20, 2003
    #4
  5. vsgdp

    Rolf Magnus Guest

    Victor Bazarov wrote:

    > "vsgdp" <> wrote...
    >> 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...

    >
    > Do you have another 'f' somewhere, in the global scope?


    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.

    > Ambiguity can only exist if there is more than one to choose from.


    There are two to choose from.
     
    Rolf Magnus, Aug 20, 2003
    #5
  6. vsgdp

    Agent Mulder Guest

    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
     
    Agent Mulder, Aug 20, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    572
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,135
  3. Anonymous
    Replies:
    3
    Views:
    534
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    450
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,367
    Howard
    Nov 29, 2004
Loading...

Share This Page