A message appears that there is no default constructor.

Asked 2 years ago, Updated 2 years ago, 56 views

Point class, which stores coordinates of points, and Rectangle class that inherits Shape.

#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;

class Point
{
    int x, y;
public:
    Point(int a, int b)
    {
        this->x = a;
        this->y = b;
    }
    Point(Point &A)
    {
        this->x = A.x;
        this->y = A.y;
    }

};
class Shape
{
public:
};
class Rectangle : public Shape
{
    Point p[4];
public:
    Rectangle(const Point p1, const Point p2, const Point p3, const Point p4)
       {
        p[0] = p1;
            p[1] = p2;
            p[2] = p3;
            p[3] = p4;
    }

Rectangle(const Point p1, const Point p2, const Point p3, const Point p4)

In this part, there is an error that there is no default generator when transferring parameters, which generator should I create?

c++ class constructor

2022-09-21 17:55

1 Answers

This error is caused by the lack of a default constructor in the Point class.

class Rectangle : public Shape
{
    Point p[4];
    // blah blah blah

You declared a point-type array, p, as a member variable of Rectangle. No default constructor can generate this p.

Change the Point class as follows.

class Point
{
    int x, y;
public:
    Point() {}
    Point(int a, int b)
    {
        this->x;
        this->y;
    }
    Point(Point &A)
    {
        this->x = A.x;
        this->y = A.y;
    }
};


2022-09-21 17:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.