C ++重载成员函数错误

时间:2011-08-24 13:11:45

标签: c++ visual-studio visual-studio-2010 overloading

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

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

5 个答案:

答案 0 :(得分:1)

People.cpp应该是:

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

答案 1 :(得分:0)

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

应该是

people :: people(string x,birthday bo)

而不是

人::人()

答案 2 :(得分:0)

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

你忘记了这个构造函数的参数。

答案 3 :(得分:0)

您没有实现people(string x, birthday bo);构造函数。在你的PEOPLE.cpp中,改变

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

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

答案 4 :(得分:-1)

poeple ctor声明和定义不匹配!