0

I am trying to create a class Line which consists of two objects of another class Point:

class Point
{
    double x, y, z;

    public:
    // constructor from 3 values
    Point(double x, double y, double z);

    // copy constructor
    Point(const Point &p);

    // method display
    void display();
};

// constructor from 3 values
Point::Point(double x, double y, double z)
: x(x), y(y), z(z)
{}

// copy constructor
Point::Point(const Point &p)
: x(p.x), y(p.y), z(p.z)
{}

void Point::display()
{
    cout << "Point(" << x << ", " << y << ", " << z << ")\n";
}


class Line
{
    Point pnt1, pnt2;

    public:
    // constructor from 2 points
    Line(Point& pnt1, Point& pnt2);

    // method display line
    void display();
};

// constructor from 2 points
Line::Line(Point& pnt1_, Point& pnt2_)
: pnt1(pnt1_), pnt2(pnt2_)
{}

// method display line
void Line::display()
{
    cout << "Line(Point(" << pnt1.x << ", " << pnt1.y << ", " << pnt1.z << ")" << ", Point(" << pnt2.x << ", " << pnt2.y << ", " << pnt2.z << ")\n";
}

And here is the main:

#include <iostream>
#include <cstdio>
#include "geometryitems.cpp"
using namespace std;

int main()
{
    // initialise object Point
    cout << endl << "Point initialisation:" << endl;

    Point pnt = Point(0.0, 0.0, 0.0);
    cout << "pnt = "; pnt.display();

    Point pnt2 = Point(1.0, 1.0, 1.0);
    cout << "pnt2 = "; pnt2.display();

    // initialising object Line
    cout << "Line initialisation:" << endl;

    Line line = Line(pnt, pnt2);
    line.display();

    return 0;
}

The points work fine but the line gives me errors that class "Point" has no members named a1, b1, c1, a2, b2, c2.

How to create class Line using objects of class Point? Thank you.

Update

I have updated the code using the copy constructor but it still talking about private x, y, z. Any ideas?

4
  • Declare Pointmembers as public, and use x, y, z. a1, b1 etc. are not members of Point. Commented Dec 3, 2013 at 13:37
  • There's no such public members Commented Dec 3, 2013 at 13:37
  • Your constructor for the line class is the problem. Change it Commented Dec 3, 2013 at 13:38
  • I do not want to make them public. Is there another way? Commented Dec 3, 2013 at 14:06

4 Answers 4

2

Add a copy-constructor to your Point class:

class Point
{
public:
    Point(const Point& p)
        : x(p.x), y(p.y), z(p.z)
        {}

    ...
};

Then you can use that for the Line constructor:

Line::Line(const Point& p1, const Point& p2)
    : pnt1(p1), pnt2(p2)
{}

The Line class don't need separate point variables a1, b1, c1 etc. And of course the actual Point objects don't have member variables named like that, which you try to access. Also note that the member variable in Point are private, you the Line::display function can't access them.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for your answer. Where should I insert the copy-constructor?
It says expected unqualified id before const
It says In file included from testGI.cpp:3:0: geometryitems.cpp:116:1: error: prototype for 'Line::Line(const Point&, const Point&)' does not match any in class 'Line' In file included from geometryitems.cpp:2:0,from testGI.cpp:3: geometryitems.h:97:7: error: candidates are: Line::Line(const Line&) geometryitems.h:105:5: error: Line::Line(Point&, Point&)
@Alexandr It's because I made the arguments const, you have to change the declaration or remove the const.
I get many different errors. Can you write completely both classes in your answer - I do not know how to implement your solution in my program.
|
0

You can get a quick and dirty solution with getters for your private data members.

   double getX() { return x ; };
   double getY() { return y ; };
   double getZ() { return z ; };

I changed as well the Line cstor.

   // constructor from 2 points
   Line::Line(Point& _pnt1, Point& _pnt2)
   : pnt1(_pnt1), pnt2(_pnt2)
   {}

And changed the call to these getters in your cout.

   cout << "Line(Point(" << pnt1.getX() << ", " << pnt1.getY() << ", " << pnt1.getZ() << ")" << ", Point(" << pnt2.getX() << ", " << pnt2.getY() << ", " << pnt2.getZ() << ")\n";

Check it out: http://ideone.com/aHmin9

1 Comment

Thank you for your answer. I will check that.
0

Class point has member x,y,z instead of a1,b1,c1. It should be

void Line::display()
{
    cout << "Line(Point(" << pnt1.x << ", " << pnt1.y << ", " << pnt1.z << ")" << ",  Point(" << pnt2.x << ", " << pnt2.y << ", " << pnt2.z << ")\n";
}

Comments

0

Your Question has the solution in it : class "Point" has no members named a1, b1, c1, a2, b2, c2.

So your class Contains x, y and z instead....Try to print it using x, y and z

Make sure the members are public or use getters and setters.

1 Comment

Thank you for your answer. The members are private and I do not want to make them public. I will check the second thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.