기본 생성자가 없다는 메세지 발생합니다.

조회수 14631회

점의 좌표를 저장하는 Point class와 Shape를 상속받는 Rectangle클래스 입니다.

#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)

이 부분에서 매개변수 전달시 기본 생성자가 없다는 오류가 뜨는데, 어떤 생성자를 만들어야 할까요?

1 답변

  • 좋아요

    2

    싫어요
    채택 취소하기

    Point 클래스에 default constructor ( 아무 인자도 받지 않는 constructor ) 가 없어서 발생하는 에러입니다.

    class Rectangle : public Shape
    {
        Point p[4];
        //어쩌구저쩌구
    

    Rectangle 의 멤버 변수로 Point 형 배열, p를 선언하셨는데요. 이 p를 생성할 수 있는 default 생성자가 없네요.

    Point 클래스를 다음과 같이 바꿔보세요.

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

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)