Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For a quick fix, make that <code>operator-</code> a free function instead of a friend of <code>Vector3D</code>. Friendship is only needed when you want to access <code>private</code> members, which you not do for <code>Point3D</code> (you use the public getters), and which you especially not do for the <code>Vector3D</code> (you use the constructor to directly construct it from the values).</p> <p>The same holds true for <code>operator+</code>, no need for friendship here.</p> <p>Now on to why your code fails:</p> <p>If a <code>friend</code> declaration is <em>first</em> found in a class body, and it isn't declared in the enclosing namespace after the class declaration, then the function can only be found by argument dependant lookup, meaning that it won't be found if none of the arguments are of the class type it is a friend of. You can fix this by either making them free functions when the friendship isn't needed, or by explicitly declaring them outside the body of the <code>Vector3D</code> class again in the header, e.g.:</p> <pre><code>class Vector3D{ friend const Point3D operator+(const Point3D&amp; a, const Vector3D&amp; b); friend const Vector3D operator-(const Point3D&amp; a, const Point3D&amp; b); public: // bla bla, yadda yadda }; // also *declare* them here const Point3D operator+(const Point3D&amp; a, const Vector3D&amp; b); const Vector3D operator+(const Point3D&amp; a, const Point3D&amp; b); </code></pre> <p>For those language lawyer freaks out there, here is the relevant standard paragraph:</p> <p><code>§7.3.1.2 [namespace.memdef] p3</code></p> <blockquote> <p>[...] If a <code>friend</code> declaration in a nonlocal class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. <strong>The name of the friend is not found by unqualified lookup or by qualified lookup until a matching declaration is provided in that namespace scope</strong> (either before or after the class definition granting friendship). [...]</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload