In c++, you get an error writing and building along with the example 'Normal Person Class with Deep Copy Producers' ;;

Asked 2 years ago, Updated 2 years ago, 27 views

#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 << "Run copy generator. The name of the source object" << 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 << "Right after the creation of the dagger object ----" << endl;
    father.show();
    daughter.show();

    daughter.changeName("Grace");
    cout << "After changing the name of the daughter to Grace ----" << endl;
    father.show();
    daughter.show();

    return 0;
}

Error (active) No instance of constructor "Person::Person" matches E0289 argument list.
int main() { Person father(1, "Kitae") appears as the line with the error.

c++ visual-studio-2017

2022-09-22 14:03

1 Answers

int id, char* name

->

int id, const char* name

Const must be given because the string constant has been passed as an argument for the constructor.


2022-09-22 14:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.