Hello, I am trying to create a program that will read the point-slope information for two lines and determine if they intersect or are parallel. Below is the code I have:

/*Point_Slope.ccp  Reads the point and slope information for two lines and
*				   determines whether they intersect or are parallel. If
*				   they intersect, finds the point of intersection and
*				   determines if they are perpendicular to each other.
*
* Specification:
* Input(screen):    point1 (x & y coordinates), slope1 (m1), 
*					point2 (x & y coordinates), slope2 (m2).
* Output(screen):   intersect/parallel, if perpendicular.
*****************************************************************************/

#include <iostream>                    // cin, cout, >>, <<
#include "CartesianPoint.h"
#include "Line.h"
using namespace std;
 
int main()
{
  CartesianPoint p1, p2;
  double slope1, slope2;
 
  cout << "Enter point and slope info. for line 1: ";
  cin >> p1 >> slope1;
  cout << "Enter point and slope info. for line 2: ";
  cin >> p2 >> slope2;
 
  Line line1(p1, slope1),
       line2(p2, slope2);
 
  cout << "\nline 1: ";
  line1.PointSlope(cout);
  cout << "\nline 2: ";
  line2.PointSlope(cout);
 
  if (slope1 == slope2)
     cout << "Lines are parallel\n";
  else
  {
     double xInt = (slope1 * p1.X() - slope2*p2.X() + p2.Y() - p1.Y())
                    / (slope1 - slope2),
            yInt = slope1 * (xInt - p1.X()) + p1.Y();
     CartesianPoint pInt(xInt, yInt);
     cout << "Lines intersect at " << pInt << " and they "
          << (slope1 * slope2 == -1 ? "are " : "are not ")
          << "perpendicular\n";
   }
}

/* CartesianPoint.h provides the declaration of class CartesianPoint.
 *******************************************************************/

#ifndef CARTESIANPOINT
#define CARTESIANPOINT
 
#include <iostream>
using namespace std;
 
class CartesianPoint
{
 public:
   CartesianPoint();
   CartesianPoint(double xVal, double yVal);
   double X() const;
   double Y() const;
   void SetX(double xVal);
   void SetY(double yVal);
 
 private:
   double myX, myY;
 };

inline CartesianPoint::CartesianPoint()
{ myX = 0;  myY = 0;}
 
inline CartesianPoint::CartesianPoint(double xVal, double yVal) 
{ myX = xVal;  myY = yVal;}
 
inline double CartesianPoint::X() const { return myX;}
 
inline double CartesianPoint::Y()  const { return myY;}
 
inline void CartesianPoint::SetX(double xVal) {myX = xVal;}
 
inline void CartesianPoint::SetY(double yVal) {myY = yVal;}
 
inline ostream & operator<<(ostream & out, CartesianPoint & point)
{
   out << "(" << point.X() << ", " << point.Y() << ')';
   return out;
 }
 
inline istream & operator>>(istream & in, CartesianPoint & point)
{
   char punc;
   double x, y;
   in >> punc >> x >> punc >> y >> punc;
   point.SetX(x);
   point.SetY(y);
   return in;
 }
 
#endif

/* LineSegment.h provides the declaration of class LineSegment.
 *************************************************************/

#ifndef LINESEGMENT
#define LINESEGMENT
 
#include "CartesianPoint.h"
#include <iostream>
using namespace std;
 
class LineSegment
{
 public:
   double Slope();
 
   void Print(ostream & out);
   void Read(istream & in);
   void FindPerpBisector();
 
 private:
   CartesianPoint first, last;
   double slope;
};
 
inline double LineSegment::Slope()
{
   if (first.X() == last.X())
     {
      cerr << "No slope -- line segment is vertical\n";
      return 1E38;
    }
   return (last.Y() - first.Y()) / (last.X() - first.X());
 }
 
inline void LineSegment::Read(istream & in)
{ 
   in >> first >> last;
 }
 
 
inline ostream & operator<<(ostream & out, LineSegment & seg)
{
   seg.Print(out);
   return out;
 }
 
inline istream & operator>>(istream & in, LineSegment & seg)
{
   seg.Read(in);
   return in;
 }
 
#endif

/* LineSegment.cpp defines the nontrivial members of class LineSegment.
 *********************************************************************/

#include "LineSegment.h"
 
void LineSegment::Print(ostream & out)
{ 
   if (first.X() == last.X())
      cout << "x = " << first.X();
   else
   {
      out << "y - " << first.Y() << " = " 
          << Slope() << "(x - " << first.X() << ")";
   }
}
 
void LineSegment::FindPerpBisector()
{ 
   LineSegment perpBis;
   CartesianPoint
      midPt(first.X() + last.X())/2.0), (first.Y() + last.Y())/2.0);
 
   perpBis.first = midPt;
   
   perpBis.last.SetX(1 + midPt.X());
   perpBis.last.SetY(-1.0 / Slope() + midPt.Y());
 
   cout << perpBis;
}

/* Line.h provides the declaration of class Line.
 ***************************************************************/

#ifndef LINE
#define LINE
 
#include "CartesianPoint.h"
#include "LineSegment.h"
 
#include <iostream>
using namespace std;
 
class Line
{
 public:
   Line(CartesianPoint p, double m);
   void PointSlope(ostream & out);
   void SlopeIntercept(ostream & out);
 
 private:
   CartesianPoint point;
   double slope;
 };
 
 
inline Line::Line(CartesianPoint p, double m)
{ 
   point.SetX(p.X());
   point.SetY(p.Y());
   slope = m;
 }
 
inline void Line::PointSlope(ostream & out)
{
   out << "y - " << point.Y() << " = "
          << slope << "(x - " << point.X() << ")" << endl;
 }
 
inline void Line::SlopeIntercept(ostream & out)
{
   out << "y = " << slope << "*x + " 
        << -slope*point.X() + point.Y() << endl;
 }
 
#endif

I am getting the error: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'CartesianPoint' (or there is no acceptable conversion), along with another. But, I would like to ask for help with this one.

Any assistance would be greatful....thanks in advance.

Recommended Answers

All 14 Replies

Which of the 232 lines does that error occur on ?

Sorry about that. I forgot to add that the error occurs at line 140 of what I have posted.

The compiler doesn't know how to output the CartesianPoint class. You probably need to write an overload operator >> for it. You didn't post that class so we have no idea how to help you.

line 124 looks a bit strange.
CatesianPoint is a class, but you are using it like a typedef?

line 124 looks a bit strange.
CatesianPoint is a class, but you are using it like a typedef?

That line is ok. Class objects are declared like that.

I was going to make lines 71-101 the CartesianPoint.cpp, but, thought against it. Should I convert these lines into a seperate file to overload the CartesianPoint operator?

That line is ok. Class objects are declared like that.

Forgive me. I'm trying to "help AND learn"
line 92 appears to have the overload that you mentioned.
It passes the CartesianPoint by reference, but on line 23 the CartesianPoint variable is a value.
Is this why the compiler can't find a match?

Unfortunitly, I am trying to understand and learn. I am trying to get an example to work and I am not having any luck. So, I am open to suggestions. I am confused as to why the compiler can't find a match. It seems to work inside my head.....go figure.

The problem is elsewhere. I tried to compile with VC++ 2008 Express and got errors on FindPerpBisector(). When I recoded like this everything compiled ok. The problem seems to be mismatched parenthases.

void LineSegment::FindPerpBisector()
{ 
   LineSegment perpBis;
   double x = (first.X() + last.X())/2.0;
   double y = (first.Y() + last.Y())/2.0;
   CartesianPoint midPt(x,y);

<snip>

Another suggestion: you header files and class declarations would look a lot better by actually using inline code in the class instead of writing separate functions using the inline keyword

class CartesianPoint
{
 public:
    CartesianPoint() {myX = 0.0F; myY = 0.0F;}
    CartesianPoint(double xVal, double yVal)
        { myX = xVal;  myY = yVal;}
    double X() const {return myX;}
    double Y() const {return myY;}
   void SetX(double xVal) {myX = xVal;}
   void SetY(double yVal) {myY = yVal;}
 
 private:
   double myX, myY;
 };

When I comment out lines 138-141 I get an error at line 178 that says "... error C2664: 'CartesianPoint::CartesianPoint(const CartesianPoint &)' : cannot convert parameter 1 from 'double' to 'const CartesianPoint &' ". To me, the initial error is cause by something very simple to be missing. But, I can't see the "tree thru the forest"....Anyone got an eagle's eye as to the cause?

When I comment out lines 138-141 I get an error at line 178 that says "... error C2664: 'CartesianPoint::CartesianPoint(const CartesianPoint &)' : cannot convert parameter 1 from 'double' to 'const CartesianPoint &' ". To me, the initial error is cause by something very simple to be missing. But, I can't see the "tree thru the forest"....Anyone got an eagle's eye as to the cause?

Yup -- the problem has nothing to do with lines 138-142. Read my previous post for the correction to that error. The problem is mismatched parentheses.

Thanks for looking into this. I appreciate the insight. In fact, my first thought was that there was something wrong with the parentheses in LineSegment::FindPerpBisector(). I have change the code per your but I am still recieving the error at line 140. You say that you were able to compile after making the changes? Was there anything else you changed? I also like the consolidation you suggested on getting rid of some of the inline code. Now I'm down to 2 errors....the original and something about not being able to open one of my include files "Line.h". That's a new one...must be a system hang up.

I made no other changes. Just to make sure, I copied it all over again and just made that one change. I used VC++ 2008 Express. What compiler are you using?

Attached is my project if it will help you.

Thankyou very much for all of your help. I am using Microsofts latest and greatest compilier Visual Studio 2005. I can't readily see a difference with what you had versus what I had, but, I plan on a comparison(s) of the print outs (after the fixing). When I bring yours over it runs like it is suppose to. I'll still play with mine to get a good understanding as to how this all works. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.