Title: How do I declare the objects related to one class in other?
Tags: c++;class;oop;variables;inner-classes
Question: The error shows: no matching functions for call to birthday75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0irthday(). How else should I declare the objects of one class in another class?
#include <iostream&gt;
using namespace std;
class birthday
public:
birthday(int d,int m,int y)
date=d;
month=m;
year=y;
void printdate()
cout<<date<<&quot;/&quot;<<month<<&quot;/&quot;<<year<<endl;
private:
int date;
int month;
int year;
class person
public:
person(string x)
name=x;
void printname()
cout<< &quot;the birthday person is &quot;<< name << &quot; whose birthday is on &quot;<< day.printdate();
private:
birthday day;
string name;
int main()
birthday b(10,11,2007);
person p(&quot;Jessica&quot;);
p.printname();
return 0;
Comment: Unless you modify `birthday` so that there is a constructor that can be called with 0 arguments, you need to use the initializer list to pass the constructor arguments or add a default initialization: `person(string x): day(1, 1, 1970), name(st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0move(x)) {}` or `birthday day { 1, 1, 1970};`

Comment: When a `person` is constructed, what do you want `birthday day` to hold?  Currently, a `birthday` can not be created without supplying a day, month, and year.


Here is another answer: Just define a default constructor so you can make an object of type birthday without initializing it.
or  you can default initialize person type private data member day.

Here is another answer: While the answers given regarding adding a default c'tor for birthday are technically correct, I think that they miss the real issue. Given how you declare the variables b and p in main(), it looks as if your intent is for the birthday variable b to be added to the person variable, which implies that what you actually want is to modify the person() constructor as such:
person(string x, birthday b): name(x), day(b)
{ }
This will ensure that a birthday object is defined for a given person object when the person is constructed.
This isn't to say that adding a default c'tor for either birthday or person is wrong; indeed, it would probably be advisable. But on its own it might not have the desired effect.

Here is another answer: The problem is that your class birthday has a user-defined constructor and therefore the compiler will not synthesize the default constructor birthday75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0irthday() for your class.
So when you wrote,
class person
//other members here
private:
birthday day; //this need the default constructor of birthday. But since class birthday has no default constructor this statement gives error
In the above snippet, the statetment, birthday day; needs the default constructor of class birthday but since there isn't one, so this statement results in the mentioned error.
Solution
To solve this you can add a default constructor for your class birthday as shown below:
class birthday
public:
//add default constructor
birthday() = default;
birthday(int d,int m,int y)
date=d;
month=m;
year=y;
void printdate()
cout<<date<<&quot;/&quot;<<month<<&quot;/&quot;<<year<<endl;
private:
//using in class initializers give some default values to the data members according to your needs
int date = 0;
int month =0;
int year = 0;
class person
public:
person(string x)
name=x;
void printname()
cout<< &quot;the birthday person is &quot;<< name << &quot; whose birthday is on &quot;;

day.printdate();//moved this out of the cout because printdat() does not return anything
private:
birthday day;
string name;
int main()
birthday b(10,11,2007);
person p(&quot;Jessica&quot;);
p.printname();
return 0;
Some of the changes that i made include:
Added default constructor for class birthday.
Used in-class initializers for data member date, year and month inside class birthday.
There was another problem with your code. Originally you had a statement cout<<day.printdate() in your program. But since printdate() has return type void, so the statement cout<<day.printdate() will give you error. To solve this, i removed the cout from this statement.
The output of the modified program can be seen here.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:02/09/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><class><oop><variables><inner-classes>', 'Title': 'How do I declare the objects related to one class in other?', 'ViewCount': 107, 'id': 71057354, 'user_AboutMe': None, 'user_AccountId': 22980709.0, 'user_DisplayName': 'Anjali Sharma', 'user_Reputation': 3.0, 'user_Views': 5.0, 'user_id': 17107678.0}}