Mark S
Mark S

Reputation: 235

Forward declaration and friend function

Following the question here if I omit the namespace like this:

void f(window);

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

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

I get strange behavior:

friend void f(window);

Compiles without forward declaration of f(window), but

friend void ::f(window);

Does not:

error C2039: 'f' : is not a member of '`global namespace''

Can someone explain the reason for it? Why does :: makes this difference, if we are in the global namespace anyway...

Thank?

Upvotes: 1

Views: 512

Answers (1)

If you don't qualify f in the friend declaration, it also behaves like a normal declaration and declares f in the surrounding namespace (global in your case).

However, if you explicitly qualify it as ::f, it is no longer a declaration of f, but only a friend declaration which wants to reference an already declared f. But there is none, hence the error.

Upvotes: 1

Related Questions