The error was "no instance of overloaded function "people::people" matches the specified type" on the first line of code of my C++ file. People::people... I included my cpp file first, then header file. Please let me know if you need further information.
the code is trying to pass string n to name, and bo from birthday class to dateOfBirth. Please ONLY look at code people::people(string n, string bo) and :name(n), dateOfBirth(bo). That's the only error I have. Other codes are reference.
//Header file below
#ifndef PEOPLE_H // if not to find Sally header file
#define PEOPLE_H
#include <string>
#include "birthday.h"
usingnamespace std;
class people
{
public:
people(string n, birthday bo);
void printInfo();
private:
string name;
birthday dateOfBirth;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include "birthday.h"
usingnamespace std;
birthday::birthday(int m, int d, int y)
{
month = m;
day = d;
year=y;
}
void birthday::printDate(){
cout << month << "/" << day << "/" << year << endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef BIRTHDAY_H // if not to find Sally header file
#define BIRTHDAY_H //Then define it
class birthday
{
public:
birthday(int m, int d, int y);
void printDate();
private:
int month;
int day;
int year;
};
#endif
You're trying to create a people object, using a constructor that accepts a string (actually const char* , but the conversion gets done for you) and a birthday object.
Let's see the constructor code you actually wrote:
1 2 3 4
people::people(string n, string bo)
:name(n), dateOfBirth(bo)
{
}
Well this appears to be a constructor that accepts a string and another string. Where is the code implementing the constructor that accepts a string and a birthday?
i changed people::people(string n, string bo) (7th line of first chunk of code) to people::people(string n, birthday bo). The error was gone but the code doesn't run. The error message is undefined reference to `birthday::printDate()
& undefined reference to `birthday::birthday(int,
int, int)
#include <string>
#include <iostream>
class birthday
{
public:
birthday(int m, int d, int y);
void printDate();
private:
int month {};
int day {};
int year {};
};
class people
{
public:
people(const std::string& n, const birthday& bo);
void printInfo();
private:
std::string name;
birthday dateOfBirth;
};
birthday::birthday(int m, int d, int y) : month(m), day(d), year(y) {}
void birthday::printDate() {
std::cout << month << "/" << day << "/" << year << '\n';
}
people::people(const std::string& n, const birthday& bo) : name(n), dateOfBirth(bo) {}
void people::printInfo() {
std::cout << name << " was born on ";
dateOfBirth.printDate();
}
int main()
{
birthday birthdayObj(1, 11, 1994);
people JunweiC("junwei the king", birthdayObj);
JunweiC.printInfo();
}