C++重载成员函数错误

发布于 2024-11-30 22:54:41 字数 1814 浏览 1 评论 0原文

你好,我正在制作一个包含 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();
    }

BIRTHDAY.h

    #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;
    }

PEOPLE .h

    #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(); 
    }

hi i was making a program with 3 classes and when i was using a member initialization list i got an error saying "no instance of overloaded function "people::people" matches the specified type:

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();
    }

BIRTHDAY.h

    #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;
    }

PEOPLE.h

    #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;
    };

PEOPLE.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(); 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巨坚强 2024-12-07 22:54:41

People.cpp 应该是:

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

People.cpp should be:

people::people(string x, birthday bo) : name(x), dateOfBirth(bo) { } 
本王不退位尔等都是臣 2024-12-07 22:54:41

您尚未实现 people(string x,birthday bo); 构造函数。在您的 PEOPLE.cpp 中,更改

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

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

You havn't implemented the people(string x, birthday bo); constructor. in your PEOPLE.cpp, change

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

to

people::people(string x, birthday bo)
    : name(x), dateOfBirth(bo)
夏末染殇 2024-12-07 22:54:41

PEOPLE.cpp 中的构造函数有错误的签名:

它应该是:

people::people(string x, birthday bo)

而不是:

people::people()

Your constructor in PEOPLE.cpp has the wrong signature:

It should be:

people::people(string x, birthday bo)

Instead of:

people::people()
旧人哭 2024-12-07 22:54:41
 people::people()
: name(x), dateOfBirth(bo)
{
}

您忘记了对该构造函数的参数。

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

You have forgotten your arguments to this constructor.

无言温柔 2024-12-07 22:54:41

人物声明和定义不匹配!

poeple ctor declaration and definition doesn't match!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文