14

Im trying to define a class friend function outside the namespace like this:

namespace A{
class window{
    private:
    int a;
    friend void f(window);
};
}

 void f(A::window rhs){
 cout << rhs.a << endl;
}

Im getting an error said that there is ambiguity. and there is two candidates void A::f(A::window); and void f(A::window). So my question is :

1) How to make the global function void f(A::window rhs) a friend of the class A::window.

EDIT: (After reading the answers)

2) why do I need to qualify the member function f inside window class to be global by doing ::f(window) ?

3) why do I need to predeclare the function f(A::window) in this particular case, whereas when the class is not a defined inside a namespace it's okey for the function to be declared after the function is declared a friend.

jleahy
  • 16,149
  • 6
  • 47
  • 66
AlexDan
  • 3,203
  • 7
  • 30
  • 46

3 Answers3

25

As well as adding a :: you need to forward declare it, e.g.:

namespace A { class window; }

void f(A::window);

namespace A{
  class window{
    private:
    int a;
    friend void ::f(window);
  };
}

void f(A::window rhs){
  std::cout << rhs.a << std::endl;
}

Note that for this forward declaration to work you need to forward declare the class too!

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • so the :: prefix to access global namespace is the one that required pre declared function void f(A::window); is this correct. thanks in advance. – AlexDan Jun 07 '12 at 15:06
  • Yes this works when the function returns void. But what if it returns an instance of a class, say B? Then the compiler barfs on "friend B ::f()" because it thinks we mean "B::f()" (i.e. a function of class B, as opposed to a global function). Any solution to that? – David Faure Apr 19 '16 at 07:55
  • I don't see how you can end up with a name clash like that legally - can you show it on something like ideone? – Flexo Apr 19 '16 at 08:01
  • 1
    It's not a clash, but a parse error. error: no ‘int B::f(A::window)’ member function declared in class ‘B’ http://www.davidfaure.fr/2016/friend_func.cpp – David Faure Apr 19 '16 at 13:24
  • 5
    @DavidFaure A bit late, but I just had the same issue. Adding parentheses did the trick: `friend B (::f(window));`. (see https://ideone.com/x38T88 for sample) – Samuel Delisle Oct 21 '19 at 19:02
5

This should do it: you need forward declares to make it clear f is in the global namespace (and not file static):

#include <string>
#include <iostream>

using namespace std;

////// forward declare magic:

namespace A{ class window; }    
void f(A::window rhs);

//////

namespace A {
    class window {
        private:
            int a;
            friend void ::f(window);
    };
}

void f(A::window rhs) {
    cout << rhs.a << endl;
}

int main()
{
}
sehe
  • 374,641
  • 47
  • 450
  • 633
0

Using ::, you can also friend a class from the outer wrapping namespace:

namespace Na {
class A;
namespace Nb {
class B {
...
   friend class ::Na::A;
};

Without going from global top as 'friend class Na::A', the search will be done in Na::Nb::Na only.

Spock77
  • 3,256
  • 2
  • 30
  • 39