c++ constructor questions

Asked 2 years ago, Updated 2 years ago, 51 views

#include <iostream>
#include <cstring>

using namespace std;

namespace COMP_POS {
    enum {
        CLERK = 1, SENIOR, ASSIST, MANAGER
    };

    void ShowPositionInfo(int pos)
    {
        switch (pos) {
        case CLERK:
            cout << "Employee" << endl;
            break;
        case SENIOR:
            cout << "Director" << endl;
            break;
        case ASSIST:
            cout << "Assistant Manager" << endl;
        case MANAGER:
            cout << "Exaggeration" << endl;
        }
    }
}

class NameCard {
private:
    char * name;
    char * company;
    char * phone;
    int position;
public:
    NameCard(char * name, char * company, char * phone, int mypos) : position(mypos)
    {
        this->name = new char[strlen(name) + 1];
        strcpy(this->name, name);
        this->company = new char[strlen(company) + 1];
        strcpy(this->company, company);
        this->phone = new char[strlen(phone) + 1];
        strcpy(this->phone, phone);
    }
    NameCard(const NameCard &copy) : position(copy.position)
    {
        name = new char[strlen(copy.name) + 1];
        strcpy(name, copy.name);
        company = new char[strlen(copy.company) + 1];
        strcpy(company, copy.company);
        phone = new char[strlen(copy.phone) + 1];
        strcpy(phone, copy.phone);
    }
    void ShowNameCardInfo()
    {
        cout << "Name:" << name << endl;
        cout << "Company: " << company << endl;
        cout << "Phone number:" << phone << endl;
        cout << "Status: ";
        COMP_POS::ShowPositionInfo(position);
        cout << endl << endl;
    }
    ~NameCard()
    {
        delete[]name;
        delete[]company;
        delete[]phone;
        cout << "called destructor!" << endl;
    }
};

int main()
{
    NameCard manClerk("Lee", "ABC", "010-8200-6545", COMP_POS::CLERK);
    NameCard copy1 = manClerk;
    NameCard manSenior("Hong", "Organge", "01-8222-54545", COMP_POS::SENIOR);
    NameCard copy2 = manSenior;
    copy1.ShowNameCardInfo();
    copy2.ShowNameCardInfo();
    return 0;
}

I am studying Yoon Sung-woo's passionate c++ programming as a textbook. The above shows one of the examples, where the red lines are drawn at "Lee" and "Hong" respectively in the manClerk and manSenior parts of the main function

No instance of constructor "NameCard::NameCard" matched argument list.

The error appears...

Why does this error occur when there is nothing different from the answer in the book...?

c++ constructor

2022-09-21 20:49

1 Answers

There seems to be a problem with the textbook. If you look at the NameCard constructor, the argument is char* type, but string literal "abcd" and so on can only be received with const char*. Therefore, you must add const to all char*.


2022-09-21 20:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.