0

This is my Line2D.h

class Line2D
{
private:
    Point2D pt1;
    Point2D pt2;
protected:
    double length;
    // Set function
    void setLength();
public:
    // Constructor
    Line2D();
    Line2D(Point2D pt1, Point2D pt2);
    // Get functions
    Point2D getPt1();
    Point2D getPt2();
    double getScalarValue();
    // Set functions
    void setPt1(Point2D pt1);
    void setPt2(Point2D pt2);
    // Other functions
    void printLine2D();
    bool operator==(const Line2D& otherL2D) const;
};

This is my Point2D.h,

class Point2D
{
// friend bool operator==(const Point2D& otherP2D) const;
 protected:
// private:
// public:
    int x;
    int y;
    double distFrOrigin;
    // This function computes the distance of
    // the point to the origin (0,0) and initializes 
    // disFrOrigin with the distance value.
    void setDistFrOrigin();
 public:
    // Constructor
    Point2D();
    Point2D(int x, int y);
    // Get functions
    int getX();
    int getY();
    // Set functions
    void setX(int);
    void setY(int);
    // Accessor method(returns the value of distFrOrigin
    double getScalarValue();
    void printPoint2D();
    bool operator==(const Point2D& otherP2D) const;
 };

This is my Line2D.cpp,

Line2D::Line2D()
{
pt1 = Point2D();
pt2 = Point2D();
length = 0.0;
}
Line2D::Line2D(Point2D pt1, Point2D pt2):Point2D(x,y)
{
/*
// Trial 1
pt1.x = Point2D.getX();
pt1.y = Point2D.getY();
pt2.x = Point2D.getX();
pt2.y = Point2D.getY();
*/ 
// pt1.Point2D(x,y);
// pt2.Point2D(x,y);
// Setting for both points
// this -> pt1 = pt1;
// this -> pt2 = pt2;
// setLength();
}
Line2D::setLength()
{
// int xL = pow((pt1.x - pt2.x),2);
// int yL = pow((pt1.y - pt2.y),2);
int xL = pow((pt1.getX() - pt2.getX()),2);
int yL = pow((pt1.getY() - pt2.getY()),2);
length = sqrt(xL + yL);
this -> length = length;
}
Line2D::getScalarValue()
{
return length;
}
Line2D::getPt1()
{
return pt1;
}
Line2D::getPt2()
{
return pt2;
}
Line2D::setPt1(Point2D pt1)
{
// Setting for first point only
this -> pt1 = pt1;
}
Line2D::setPt1(Point2D pt2)
{
// Setting for second point only
this -> pt2 = pt2;
}
void Line2D::printLine2D()
{
cout << "  " << pt1  << "  " << pt2 << "  " << length << endl;
}
bool Line2D::operator==(const Line2D& otherL2D) const 
{
return(pt1 == otherL2D.pt1 &&
        pt1 == otherL2D.pt2);
}

this is my code for Point2D.cpp,

`Point2D::Point2D()
{
x = 0;
y = 0;
}

`

Point2D::Point2D(int x, int y)
{
this -> x = x;
this -> y = y;
}

void Point2D::setX(int x)
{
this -> x = x;
}
void Point2D::setY(int y)
{
this -> y = y;
}
int Point2D::getX()
{
return x;
}
int Point2D::getY()
{
return y;
}
void Point2D::setDistFrOrigin()
{
distFrOrigin = sqrt( pow(x,2) + pow(y,2) );
}
void Point2D::printPoint2D()
{
cout << "  " << x  << "  " << y << "  " << distFrOrigin << endl;
}
bool Point2D::operator==(const Point2D& otherP2D) const 
{
return(x == otherP2D.x &&
        y == otherP2D.y);
}

Here's my problem: My Point2D works fine. Line2D encapsulates Point2D. How do I write the constructor and the setLength() function? The error I am getting is within these 2:

  1. In Line2D.cpp: Point2D is not a direct base of Line2D
  2. In Line2D.cpp: ISO C++ forbids declaration of 'setLength' with no type
  3. In Line2D.cpp: setLength() cannot be overloaded (qn, what do i need to overload and how?)
  4. In Line2D.cpp: call of overloaded 'pow(int,int)' is ambiguous (Don't understand this)

All the include header etc. is correct so I didn't put them here.

Those are the errors I am getting. I will be grateful for any help.

Thank you!

Jon Heller
  • 34,999
  • 6
  • 74
  • 132
  • You must specify the return type in each function declaration, including definitions. Thus, no `Line2D::setLength(...) {...}` but `void Line2D::setLength(...) {...}` – n. m. could be an AI Nov 14 '16 at 06:52
  • Pow problem: http://stackoverflow.com/questions/17633970/building-libspline-for-matlab-on-windows-ambiguous-call-to-overloaded-function – n. m. could be an AI Nov 14 '16 at 06:59
  • Unable to reproduce most of these. Too many other errors getting in the way. #4 is almost certainly the result of [the most foul `using namepace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) though. – user4581301 Nov 14 '16 at 06:59
  • There are many design mistakes, head for [codereview.se] once you get the program to run. – n. m. could be an AI Nov 14 '16 at 07:01
  • #1 `Line2D::Line2D(Point2D pt1, Point2D pt2):Point2D(x,y)` compiler sees `Point2D(x,y)` as, since there is not member named `Point2D` to initialize, as an attempt to initialize a base class. There is no base class for `Line2D`. What are you attempting with this bit of code? – user4581301 Nov 14 '16 at 07:05
  • Might be a good idea to review the the syntax covered in the first few chapters of a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) before continuing. – user4581301 Nov 14 '16 at 07:08

1 Answers1

2

1. What do you mean by direct base? If you want them to share the same functions etc. you need to use abstract class, other than that, I am not sure if I understood this part of your question.

2. You need to specify "void" keyword in front of your function definition. Other than that, your setLength() function declared as "protected" in your header file. I don't know if you have a specific reason for a "set" function to be protected, but usually "set" functions are declared as publicly.

3. I couldn't see any other declaration of your "setLength()" function in your header file that is gooing to be overloaded later. You need to declare all of your functions even the ones that oyu need to overload too. It is important for compile to understand that which functions that it has.

4. You need to be more specific. I presume that this is the line that you are talking about:

int xL = pow((pt1.getX() - pt2.getX()),2);

xL = (pt1's X - pt2's X) * (pt1's X - pt2's X)

Hope this helps you.

Prometheus
  • 1,522
  • 3
  • 23
  • 41