#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.
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.
© 2024 OneMinuteCode. All rights reserved.