c++에서 '깊은 복사 생성자를 가진 정상적인 Person클래스'라는 예제를 따라 적고 빌드하는데 오류가 발생하네요;;

조회수 1449회
#include <iostream>
#include <cstring>
using namespace std;

class Person {
    char* name;
    int id;
public:
    Person(int id, char* name);
    Person(Person& person);
    ~Person();
    void changeName(const char *name);
    void show() { cout << id << ',' << name << endl; }
};

Person::Person(int id, char* name) {
    this->id = id;
    int len = strlen(name);
    this->name = new char[len + 1];
    strcpy(this->name, name);
}

Person::Person(Person& person) {
    this->id = person.id;
    int len = strlen(person.name);
    this->name = new char[len + 1];
    strcpy(this->name, person.name);
    cout << "복사 생성자 실행. 원본 객체의 이름" << this->name << endl;
}

Person::~Person() {
    if (name)
        delete[] name;
}

void Person::changeName(const char* name) {
    if (strlen(name) > strlen(this->name))
        return;
    strcpy(this->name, name);
}

int main() {
    Person father(1, "Kitae");
    Person daughter(father);

    cout << "daughter 객체 생성 직후 ----" << endl;
    father.show();
    daughter.show();

    daughter.changeName("Grace");
    cout << "daughter 이름을 Grace로 변경한 후 ----" << endl;
    father.show();
    daughter.show();

    return 0;
}

오류(활성) E0289 인수 목록이 일치하는 생성자 "Person::Person"의 인스턴스가 없습니다.
int main() { Person father(1, "Kitae") 이부분이 오류가 발생한 줄이라고 뜹니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • int id, char* name
    
    ->
    
    int id, const char* name
    

    문자열 상수를 생성자의 인수로 전달했기 때문에 const를 붙여주어야 합니다.

    • (•́ ✖ •̀)
      알 수 없는 사용자

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

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

(ಠ_ಠ)
(ಠ‿ಠ)