C++ 인수 목록이 일치하는 생성자의 인스턴스가 없습니다.

조회수 9310회
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>

using namespace std;

enum Menu { continent_search = 1, hwak, die, wanchi, EXIT };
enum Menu2 { continent_choice = 1, GO_MAIN };

class country
{
public:
    string name;
    int hwakjin, die, wanchi;
    country(string name, int hwakjin, int die, int wanchi) {
        this->name =  name;
        this->hwakjin =  hwakjin;
        this->die = die;
        this->wanchi = wanchi;

    }
    bool operator <(country& country) {

        return this->hwakjin > country.hwakjin;
        return this->wanchi > country.wanchi;
        return this->die > country.die;
    }
};


class Covid
{
private:

    country countries[9] = {
       country("한국",4,1,1),
       country("중국",8,3,4),
       country("일본",6,7,8) }; // 오류가 생기는 부분 


public:


    Covid() {}

    int showhwak(void)
    {
        sort(countries, countries + 3);
        for (int i = 0; i < 3; i++)
        {
            cout << countries[i].name << "사망자" << countries[i].hwakjin << endl;
        }
        return 0;
    }
    void showTitle()
    {
        cout << "코로나 확진자 현황\n";
    }

    void showMenu()
    {
        cout << "==================\n";
        cout << "1. 대륙검색\n";
        cout << "2. 확진자수\n";
        cout << "3. 사망자수\n";
        cout << "4. 완치자수\n";
        cout << "5. 종료\n";
        cout << "==================\n";
    }

    void showcontinentmenu()
    {
        cout << "==================\n";
        cout << "1. 아시아\n";
        cout << "2. 아프리카\n";
        cout << "3. 메인으로\n";
    }

    void continent()
    {
        while (true) {

            showcontinentmenu();
            int menuNum = inputMenu();
            switch (menuNum) {
            case continent_choice:
                asia();
                break;
            case GO_MAIN:
                return; break;
            default:
                break;
            }
        }
    }

    int inputMenu() {
        int menuNum = -1;
        cout << "입력>>";
        cin >> menuNum;
        return menuNum;
    }

    void _exit() {
        exit(0);
    }

    void asia()
    {
        string name;
        int temp;
        cout << "아시아 나라를 입력하세요:\n";
        cin >> name;
        for (int i = 0; i < 3; i++) {
            if (name == countries[i].name)
                i = temp;
        }
        cout << countries[temp].name << "사망자" << countries[temp].hwakjin << "확진자" << countries[temp].wanchi << endl;
    }

    void showsamang()
    {


    }
    void showwanchi()
    {


    }

    void showcountry()
    {

    }

    void initialize()
    {
        showTitle();
        while (true) {
            showMenu();
            int menuNum = inputMenu();

            switch (menuNum)
            {
            case continent_search:
                continent();
                break;
            case hwak:
                showhwak();
                break;
            case EXIT:
                _exit();
                break;
            default:
                break;

            }
        }
    }
};

int main()
{
    Covid covid = Covid();
    covid.initialize();
}

오류가 생기는 부분에서 country countries[9]중 []안에 9를 지워주면 불완전한 형식이라고 에러가 발생하고 9를 써주면 C++ 인수 목록이 일치하는 생성자의 인스턴스가 없습니다. 라는 에러가 발생하는데 뭐가 문제인 걸까요 ㅠㅠ

  • 올려주신 코드 country 부분만 떼서 간단하게 돌려보니 9 없이 실행했을때 실행이 되던데요? 암튼 '인수 목록이 일치하는 생성자의 인스턴스가 없습니다' 라는 에러는 길이가 9인 배열을 선언했는데 값을 3개만 넣어서 그런 것 같습니다. HIAOAIH 2020.6.13 23:23

1 답변

  • country countries[9] = {
           country("한국",4,1,1),
           country("중국",8,3,4),
           country("일본",6,7,8) };
    

    해당 코드의 countries는 9개의 원소를 갖는 배열이 됩니다. 변수에 3개의 원소에 대해 값을 할당하였으므로 6개의 원소는 기본 생성자를 통해 생성됩니다.

    하지만 country 클래스에 country(string name, int hwakjin, int die, int wanchi) 생성자를 정의하셨기에 기본 생성자가 삭제됩니다.

    따라서 countries 배열은 country의 기본생성자가 없기에 생성될 수 없습니다.

    아래와 같이 파라미터의 기본 값을 설정함으로써 문제를 해결할 수 있습니다.

    country(string name = {}, int hwakjin = 0, int die = 0, int wanchi = 0)
    

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

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

(ಠ_ಠ)
(ಠ‿ಠ)