Add constructor to c++ inheritance class code error

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

This is the process of adding a constructor to an existing inheritance class code.

Continue

No instance of constructor MyFriendDetailInfo::MyFriendDetailInfo matching argument list MyFriendDetailInfo:::MyFriendDetailInfo(char*, int, char*): Argument 1 is: Cannot convert from const char[7] to char*

Two of these errors appear.

I've removed the extinction

class MyFriendInfo
{
private:
    char* name;
    int age;
public:
    MyFriendInfo()                           
    {

    }

    MyFriendInfo(char* name, int age)     
    {
        int len = strlen(name) + 1;                  
        name = new char[len];


    }

    void ShowMyFriendInfo()
    {
        cout << "Name: " << name << endl;
        cout << "Age:" <<age << endl;
    }
};


class MyFriendDetailInfo : public MyFriendInfo
{
private:
    char* addr;
    char* phone;
public:
    MyFriendDetailInfo(char* name, int age, char* addr, char* phone)
        :MyFriendInfo(name, age)
    {
        int len = strlen(addr) + 1;
        addr = new char[len];
        len = strlen(phone) + 1;
        phone = new char[len];

    }

    void ShowMyFriendDetailInfo()
    {
        ShowMyFriendInfo();
                cout << "주소: " << addr << endl;

        Cout < < "Call :" < < phone < < endl < <. ; endl
    }

};

int main()
{
    Myfrienddetailinfo friend. 1 ("name", 23, "address", "010") ;

    Friend1.ShowMyFriendDetailInfo();

    return 0;

}

c++ inheritance constructor

2022-09-20 10:50

1 Answers

Because a literal string such as "Name" is a non-modifiable string, the address cannot be stored in the char * type pointer, and the address must be stored in the const char * type pointer.

MyFriendDetailInfo(char* name, int age, char* addr, char* phone)

Therefore, code such as MyFriendDetailInfoFriend1("Name", 23, "Address", "010"); needs to be modified as below.

MyFriendDetailInfo(const char* name, int age, const char* addr, const char* phone)


2022-09-20 10:50

If you have any answers or tips


© 2022 pinfo. All rights reserved.