0
class A {
  friend void display();
};

friend void display() {
  cout<<"friend";
}

int main() {
  display();
}

Works fine...

class A {
  friend void display() {
    cout<<"friend";
  }
};

int main() {
  display();
}

It shows :

display is not declared in this scope.

Why is it so ?

Mat
  • 195,986
  • 40
  • 382
  • 396

1 Answers1

4

In the first example (which should fail to compile for another reason: You can't use friend when defining the function) you define the function display to be in the global scope.

In the second example the display function is not a member function (it's in the scope surrounding the class), but it's still declared only in the scope of the A class. You need to re-declare it in the global scope for it to actually be in the global scope.

Some programmer dude
  • 380,411
  • 33
  • 383
  • 585