簡體   English   中英

C ++重載成員函數錯誤

[英]C++ overloaded member function error

您好,我正在制作一個具有3個類的程序,當我使用成員初始化列表時,出現一個錯誤,提示“沒有重載函數“ people :: people”的實例與指定的類型匹配:

MAIN.cpp

    #include <iostream>
    #include "conio.h"
    #include <string>
    #include "birthday.h"
    #include "people.h"
    using namespace std;

    void main(){
        birthday birthObj (30, 06, 1987);

        people me("The King",birthObj);
        _getch();
    }

生日

    #pragma once
    class birthday
    {
    public:
birthday(int d, int m, int y);
        void printdate();
    private:
        int month;
        int day;
        int year;
    };

BIRTHDAY.cpp

    #include "birthday.h"
    #include <iostream>
    #include "conio.h"
    #include <string>

    using namespace std;

    birthday::birthday(int d, int m, int y)
    {
        month = m;
        day = d;
        year = y;
    }
    void birthday::printdate()
    {
        cout << day << "/" << month << "/" << year;
    }

    #pragma once
    #include <iostream>
    #include "conio.h"
    #include <string>
    #include "birthday.h"
    using namespace std;

    class people
    {
    public:
        people(string x, birthday bo);
        void printInfo();
    private:
        string name;
        birthday dateOfBirth;
    };

人.cpp

    #include "people.h"
    #include <iostream>
    #include "conio.h"
    #include <string>
    #include "birthday.h"
    using namespace std;

    people::people()
    : name(x), dateOfBirth(bo)
    {
    }

    void people::printInfo()
    {
        cout << name << " was born on ";
        dateOfBirth.printdate(); 
    }

People.cpp應該是:

people::people(string x, birthday bo) : name(x), dateOfBirth(bo) { } 

您尚未實現people(string x, birthday bo); 構造函數。 在您的PEOPLE.cpp中,更改

people::people()
    : name(x), dateOfBirth(bo)

people::people(string x, birthday bo)
    : name(x), dateOfBirth(bo)

您在PEOPLE.cpp中的構造函數具有錯誤的簽名:

應該

people :: people(字符串x,生日bo)

代替

人::人()

 people::people()
: name(x), dateOfBirth(bo)
{
}

您已經忘記了此構造函數的參數。

poeple ctor聲明和定義不匹配!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM