Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Add <code>line</code> as a <code>friend</code>, not just a method:</p> <pre><code> friend class line; </code></pre> <p>Other remarks:</p> <ul> <li>separate declarations from the implementations in header and implementation files.</li> <li>prefer full qualification over <code>using</code> directives (i.e. remove <code>using namespace std;</code> and use <code>std::cout</code> instead. </li> <li>prefer pass-by-reference for complex types - change <code>line(point,point);</code> to <code>line(const point&amp;, const point&amp;);</code></li> </ul> <p><strong>EDIT</strong> For educational purposes - You can't declare that specific function as friend as the code is now because there's no full definition of the <code>line</code> class. Ergo, the following is the only approach:</p> <pre><code>class point; class line { point *p1,*p2; public: line(point,point); float slope(); }; class point { int x,y; public: point(int,int); point(); friend float line::slope(); }; </code></pre> <p>You forward-declare <code>point</code> and change the <code>point</code> members in <code>line</code> to <code>point*</code> (because point isn't a complete type yet). In <code>point</code> you now have the full definition of the <code>line</code> class, so you can declare the method as friend.</p> <p>EDIT 2: For this particular scenario, it's not possible using <code>point</code> objects inside <code>line</code> because you'd need the full type. But <code>line</code> would also have to be fully defined in order to declare its member as <code>friend</code>.</p>
    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.
 

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