namespaces, classes and template friend functions

Discussion in 'C++' started by mojmir, Sep 12, 2008.

  1. mojmir

    mojmir Guest

    hello,

    i have problem with following code (see below). compiler (vs2005sp1)
    complains
    that the call to fn is ambiguous:
    error C2668: 'BB::fn' : ambiguous call to overloaded function
    could be 'void BB::fn<dummy>(T &,BB::foo &)'
    or 'void AA::fn<dummy>(T &,BB::foo &)'

    okay, i have said, and tried to be more specific about the namespace
    the fn<> lives in, i.e. from
    template<class T> friend void fn(T &, foo &);
    to
    template<class T> friend void AA::fn(T &, foo &);

    wow:
    void AA::fn(T &,BB::foo &)' : symbol cannot be defined within
    namespace 'BB'
    defined? who? where? i merely declared the function to be friend, no?

    best regards,
    mojmir


    =8<===================================================

    namespace BB {
    class foo;
    }

    namespace AA {
    template<class T> void fn(T &, BB::foo &);
    }

    namespace BB {
    class foo {
    template<class T> friend void fn(T &, foo &);
    };
    }

    namespace AA {
    template<class T> void fn (T &, BB::foo &) { }
    }

    class dummy { };

    void wmain() {
    using namespace BB;
    using namespace AA;
    foo f;
    dummy d;
    fn(d, f);
    }
     
    mojmir, Sep 12, 2008
    #1
    1. Advertising

  2. mojmir

    Zeppe Guest

    mojmir wrote:
    > hello,
    >
    > i have problem with following code (see below). compiler (vs2005sp1)
    > complains
    > that the call to fn is ambiguous:
    > error C2668: 'BB::fn' : ambiguous call to overloaded function
    > could be 'void BB::fn<dummy>(T &,BB::foo &)'
    > or 'void AA::fn<dummy>(T &,BB::foo &)'
    >
    > okay, i have said, and tried to be more specific about the namespace
    > the fn<> lives in, i.e. from
    > template<class T> friend void fn(T &, foo &);
    > to
    > template<class T> friend void AA::fn(T &, foo &);


    ehm, I guess the error refers to the call
    fn(d, f);
    that should be
    AA::fn(d, f);

    Best wishes,

    Zeppe
     
    Zeppe, Sep 12, 2008
    #2
    1. Advertising

  3. mojmir

    Zeppe Guest

    Zeppe wrote:
    > mojmir wrote:
    >> hello,
    >>
    >> i have problem with following code (see below). compiler (vs2005sp1)
    >> complains
    >> that the call to fn is ambiguous:
    >> error C2668: 'BB::fn' : ambiguous call to overloaded function
    >> could be 'void BB::fn<dummy>(T &,BB::foo &)'
    >> or 'void AA::fn<dummy>(T &,BB::foo &)'
    >>
    >> okay, i have said, and tried to be more specific about the namespace
    >> the fn<> lives in, i.e. from
    >> template<class T> friend void fn(T &, foo &);
    >> to
    >> template<class T> friend void AA::fn(T &, foo &);

    >
    > ehm, I guess the error refers to the call


    My bad, I didn't pay attention before answering. I guess the problem is
    that the friend template function declaration is a template declaration,
    and you cannot declare a function as belonging to a different namespace.
    Unfortunately, when you specify a friend template function I don't think
    there is any way to avoid making a template declaration, so I don't know
    how to solve the problem (if it can be).

    Best wishes,

    Zeppe
     
    Zeppe, Sep 12, 2008
    #3
  4. mojmir

    mojmir Guest

    On Sep 12, 11:28 am, Zeppe
    <> wrote:
    > Zeppe wrote:
    >
    > there is any way to avoid making a template declaration, so I don't know
    > how to solve the problem (if it can be).


    well fortunately in my case i can simply move the AA::fn into
    namespace BB
    thus removing ambiguity, but i wonder whether 'dragging' the function
    fn as
    injected friend into BB's namespace is correct.

    mojmir
     
    mojmir, Sep 12, 2008
    #4
    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. Adam Parkin
    Replies:
    6
    Views:
    527
    Victor Bazarov
    Apr 24, 2004
  2. Jess
    Replies:
    1
    Views:
    296
    John Harrison
    May 20, 2007
  3. Ronald Raygun
    Replies:
    5
    Views:
    509
    James Kanze
    Apr 2, 2008
  4. A L
    Replies:
    1
    Views:
    525
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. SunRaySon

    Friend functions or classes

    SunRaySon, Dec 2, 2007, in forum: Ruby
    Replies:
    4
    Views:
    140
    Ilan Berci
    Dec 3, 2007
Loading...

Share This Page