Home > OS >  How do I declare the objects related to one class in other?
How do I declare the objects related to one class in other?

Time:02-10

The error shows: no matching functions for call to birthday::birthday(). How else should I declare the objects of one class in another class?

#include <iostream>

using namespace std;

class birthday
{

public:

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

private:

    int date;
    int month;
    int year;
};

class person
{

public:

    person(string x)
    {
        name=x;
    }
    void printname()
    {
        cout<< "the birthday person is "<< name << " whose birthday is on "<< day.printdate();
    }

private:

    birthday day;
    string name;
};

int main()
{
   birthday b(10,11,2007);
   person p("Jessica");
   p.printname();
   return 0;
}

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

The problem is that your class birthday has a user-defined constructor and therefore the compiler will not synthesize the default constructor birthday::birthday() 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<<"/"<<month<<"/"<<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<< "the birthday person is "<< name << " whose birthday is on "; 
        
        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("Jessica");
   p.printname();
   return 0;
}

Some of the changes that i made include:

  1. Added default constructor for class birthday.
  2. Used in-class initializers for data member date, year and month inside class birthday.
  3. 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.

  • Related