title: Problems with forward declaration - Friend functions and line / point classes
body: <p>I have a demo program for understanding of friend function. I am stuck up with errors related to forward declaration stuff, I guess.</p>
  
  <p>I have a point class which has x &amp; y co-ordinates. The line class has two objects of point class. Now I have a function in line class which will calculate the slope of the line.</p>
  
  <p>This is my program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class point
  {
      int x,y;
  public:
      point(int,int);
      point();
      friend float line::slope();
  };
  
  point::point(int a, int b)
  {
      x=a;
      y=b;
  }
  
  point::point()
  {
  }
  
  class line
  {
      point p1,p2;
  public:
      line(point,point);
      float slope();
  };
  
  line::line(point p1, point p2)
  {
      this-&gt;p1=p1;
      this-&gt;p2=p2;
  }
  
  float line::slope()
  {
      float s;
      s=((float)p2.y-p1.y)/(p2.x-p1.x);
      return s;
  }
  
  int main()
  {
      float sl;
      point obj(5,10);
      point obj1(4,8);
      line obj3(obj,obj1);
      sl=obj3.slope();
      cout&lt;&lt;"\n slope:"&lt;&lt;sl;
      return 0;
  }
  </code></pre>
  
  <p>It is giving me compiler errors with respect to forward declarations due to the following:</p>
  
  <ol>
  <li><p>When I try to define my line class first, it does not know about the point class. Even if I <strong>forward declare the point class</strong>, that wont suffice coz to create objects of the point class, the compiler should know the size of the point class and hence the whole class itself. Understood it through explanation in this answer: <a href="https://stackoverflow.com/a/5543788">https://stackoverflow.com/a/5543788</a></p></li>
  <li><p>If I define the point class first, it needs to know the friend function slope and hence the class line.
  So I tried to provide the forward declaration for the line class and the slope function like this before defining the point class:</p></li>
  </ol>
  
  <blockquote>
    <p>class line;</p>
  
  <pre><code>float line::slope();
  
  class point
  {
      int x,y;
  public:
      point(int,int);
      point();
      friend float line::slope();
  };
  </code></pre>
  </blockquote>
  
  <p>Now this gives me the following errors:</p>
  
  <pre><code>friend1.cpp:5: error: invalid use of incomplete type ‘struct line’
  friend1.cpp:4: error: forward declaration of ‘struct line’
  friend1.cpp:13: error: invalid use of incomplete type ‘struct line’
  friend1.cpp:4: error: forward declaration of ‘struct line’
  friend1.cpp: In member function ‘float line::slope()’:
  friend1.cpp:9: error: ‘int point::y’ is private
  friend1.cpp:43: error: within this context
  friend1.cpp:9: error: ‘int point::y’ is private
  friend1.cpp:43: error: within this context
  friend1.cpp:9: error: ‘int point::x’ is private
  friend1.cpp:43: error: within this context
  friend1.cpp:9: error: ‘int point::x’ is private
  friend1.cpp:43: error: within this context
  </code></pre>
  
  <p>.3. Next I tried to separate out the point class in point.h and point.cpp and line class in line.h and line.cpp. But still here there is a dependency on each other.</p>
  
  <p>Though this should be possible theoretically, I cannot figure it out how to get it working.</p>
  
  <p>Looking out for answers.</p>
  
  <p>Thanks,</p>
  
  <p>Raj</p>
  
  <p><strong>PS: This program is an effort to demonstrate the usage of friend functions alone</strong>. Where friend functions are of two types, this is an effort to deal with the second of this kind:</p>
  
  <ol>
  <li>Friend functions which are independent.</li>
  <li>Friend functions which are members of another class.</li>
  </ol>
  
  <p>So, usage of friend classes are ruled out in this case.</p>