0

I'm rather new to the C++ language and programming as a whole. I have a question with regards to creating a constructor that contains arguments that are objects of another class.

Example Constructor of an Object:

Point::Point(int x, int y)
{
    //initiating the values of x and y...
}

Then now when I try to create a constructor that requires arguments of said Object from before, this is what I've come up with.

Line::Line(Point pt1, Point pt2):Point(x, y)
{
    //initiating the values of the class...
}

This gives me an error where 'Point' is not a direct base of 'Line'. I get what it is trying to say as Line is not a derived class of Point. But how do I create a constructor that requires arguments that are objects from another class?

Any help would be appreciated.

Regards, yxt.

EDIT: as requested by some, here is the constructor for Line:

Line::Line(Point newPt1, Point newPt2)
{
    pt1 = newPt1;
    pt2 = newPt2;
}

EDIT2: I've managed to solve the problem. Huge thanks to the elaborate examples and explanations of @user1158692 , @Brian Gradin and @Tristan Brindle.

3
  • Can you show how do you define Line class? Commented Nov 11, 2013 at 8:47
  • What does your Line class look like? Commented Nov 11, 2013 at 8:47
  • From your comments to the answers below, I don't understand what exact problem you are trying to solve. Post a SSCCE. Commented Nov 11, 2013 at 9:13

4 Answers 4

2

When you define a constructor taking certain arguments, you don't care how those are supplied or where they came from (well, for something simple like an int you don't). For an Example

Point::Point( int x, int y )

if the caller wishes to use values contained in another object or not it's up to him to get them and supply them - but this has absdolutely no bearing on how you write your constructor. So he would call the above constructor like:

Point apoint( 1, 2 );
// or:
Point apoint( anObj.GetX(), and Obj.GetY() );
// or:
Point apoint ( anObj.GetX(), anOtherObj.Y );

The syntax you have used with Lines constructor is for passing arguments to ether a member of that class or a base class of that class - in your case probably a member. To give you a nudge, here would be a good couple of constructors for your Line class, assuming your point class has a good set of constructors of its own - and if not, add them!

class Line
{
public:
    Line( const Point& p1, const Point& p1 )
    : m_Point1(p1), m_Point2(p2)
    {}

    Line( int x1, int y1, int x2, int y2) 
    : m_Point1(x1, yx), m_Point2(x2, y2)
    {}
private:
    Point m_Point1;
    Point m_Point2;
};

Called like:

Point aPoint, bPoint;
.
.
Line aline( aPoint, bPoint );
Line bline( 1, 1, 2, 2 );
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! That was very clear and concise, it helped me understand and figure out the problem that's been boggling my mind.
Happy to help. Wish someone else would gimme an upvote - I'm 1 point off 1000 rep! ;-)
1

The :Point(x, y) is unecessary:

Line::Line(Point pt1, Point pt2)
{
    //initiating the values of the class...
}

4 Comments

Thanks for answering, I've tried doing that but all it returns is an error that says "no matching function for call to Point::Point". Is this due to Line not being a derived class from Point?
You won't need to make Line a derived class. Just make sure that Point has all appropriate constructors. Off the top of my head, I would say that the most recent error message you mentioned would be caused by either the default or copy constructor being looked for and not found.
Wouldn't the constructor for Point stated above be the default constructor? I've tried creating another constructor in Point with no arguments to test this out and the error doesn't show up anymore. I don't quite understand why this is so.
When you pass the instances of Point into the default constructor for Line, they are being passed by value. As such, the copy constructor for Point will be called so that the new local variables for the default constructor of Line (ie the parameters) will have the same value as the instances you passed in.
1

I will take a wild guess that your Line class contains two Point members. Assuming they are called pt1 and pt2, you need to initialize them in the constructor initialization list:

Line::Line(Point pt1, Point pt2): pt1(pt1), pt2(pt2)
{

}

2 Comments

Thanks for replying, I've just tried doing that but it gives the same error as what I had initially. Line is not a derived class of Point, could that have something to do with it?
@yxt If you get that error, then you are not doing what I suggest. You are doing something completely different.
0

how do I create a constructor that requires arguments that are objects from another class?

Without the extra : Point(x, y),

Line::Line(Point pt1, Point pt2)
{
    //initiating the values of the class...
}

is fine just as it is, as long as the compiler has seen a definition of your Point class before it gets here (usually you'd do this including a point.h header file containing class Point.).

Edit to address comment:

You unfortunately haven't provided your Point class, but I'd imagine it looks something like this:

struct Point
{
    Point(double x, double y) : x(x), y(y) {}

    double x;
    double y;
};

and your Line looks something like so:

struct Line
{
    Line::Line(Point pt1, Point pt2);
    Point pt1;
    Point pt2;
};

What's happening is that when the compiler looks at your your Line(Point, Point) constructor, it notices you haven't given the member variables any values in your initialiser list, so it tries to use the default Point constructor (one which takes no arguments). Unfortunately, no such constructor exists, leading to the error. There are two different things you can do to get round it, but it's actually probably best to do both of them.

The first is to add a default constructor to your Point class -- this will allow you to place Points into standard library containers, which might be useful at some point.

Point::Point() : x(0), y(0) {}

will do it. This will fix your error, because now your Line constructor will use the default constructor for each point, and then (in the body of the constructor) assign values. But we can do better than that: you can use the initialiser list to copy construct the points straight away, skipping the default construction:

Line::Line(Point pt1, Point pt2) : pt1(pt1), pt2(pt2) {}

I think this is what you were trying to achieve in the first place, you just had the syntax a little wrong :-)

3 Comments

Thanks for the reply, I've tried to do this but all it returns is an error saying "no matching function for call to Point::Point". Line is not a derived class of Point. Could that have anything to do with it? Also I'm not supposed to make Line a derived class of Point. I just need to initialize Line using Point objects.
You won't need to make Line a derived class. Just make sure that Point has all appropriate constructors. Off the top of my head, I would say that the most recent error message you mentioned would be caused by either the default or copy constructor being looked for and not found.
Thanks! I've managed to figure it out the problem and it was exactly like you have mentioned. Thank you for taking the time to type it all out.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.