This is my Line2D.h

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>

#include "Point2D.h"

using namespace std;

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,

#ifndef POINT2D_H
#define POINT2D_H

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;

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;
};

#endif

This is my Line2D.cpp,

#include "Point2D.h"
#include "Line2D.h"

Line2D::Line2D()
{
    pt1 = Point2D();
    pt2 = Point2D();
    length = 0.0;
}

Line2D::Line2D(Point2D pt1, Point2D pt2):Point2D(x,y)
{
/*
    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 Point2D.cpp,

#include "Point2D.h"
// #include "Line2D.h"

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) '': ISO C++ forbids declaration of 'setLength' with no type
3) '': setLength() cannot be overloaded (qn, what do i need to overload and how?)
4) '': call of overloaded 'pow(int,int)' is ambiguous

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

Thank you!

1) The x and y members of Point2D are protected, and since Line2D doesn't inherit from Point2D, or has been friended by the Point2D class, this is expected.
2 and 3) setLength() in Line2D has no return type in the .cpp file. Even if it returns void, you still need to declare that since C++ assumes no return type indicates an int return type, and you have no int setLength() member function declared in Line2D.cpp. You make similar mistakes elsewhere. Time for some code cleanup I think.

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.