title: friend function and copy constructors
body: <p>The code shown below doesn't compile when the friend function mag() is defined inside the class, but works if defined outside class (commented). I think the difference is caused by the copy constructor used to change the argument type from A to B. Can someone explain why I should define the friend function outside?</p>
  
  <p>Moreover, if class B is a template class (adding <code>template &lt;class T&gt;</code> at the top), defining the friend function outside will also not work.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class A {
  };
  
  class B {
  public:
      B(const  A&amp; p) {
          std::cout &lt;&lt; "Copy/Conversion constructor" &lt;&lt; std::endl;
      }
      friend void mag(const B&amp; p) {
          std::cout &lt;&lt; "Mag Inside`.\n";
      }
  };
  //void mag(const B&amp; p) {
  //     std::cout &lt;&lt; "Mag Outside.\n";
  //}
  int main() {
      A a;
      mag(a);
      return 0;
  }
  </code></pre>