This is the prototype I was provided with for my Person constructor:

Person(const char * their_name, const char * email, int day, int month, int year);

In making my constructor I have tried to use base member intialization, but I am having issues, this is what my constructor looks like:

Person(const char * their_name, const char * email, int day, int month, int year): name(0), email_address(0), aDay(1), aMonth(1), aYear(2000){

        char * temp = new char[strlen(their_name) + 1]; 
        strcpy_s(temp, strlen(their_name) + 1, their_name); 
        name = temp; 
        email_address = new char[strlen(email) + 1]; 
        strcpy_s(email_address, strlen(email) + 1, email); 
        /*
        int aDay = day;
        int aMonth = month;
        int aYear = year;
        */
        cout << "\nPerson(...) CREATING: ";
        printOn(cout);
    }

My compiler is having issues with saying aDay(1), aMonth(1), aYear(2000). If this is not how I am supposed to initialize these three ints, how am I supposed to?

This is my Date class that have a constructor that takes 3 ints...

public: 
    //Notice there is no default constructor 
    Date(int d, int m, int y) {day = d; month = m; year = y;} 

    void printOn(ostream & out) const {out << day <<"/" << month << "/" << year;}

    ~Date(){
    cout<<"Deleting Date: "<<day<<"/"<<month<<"/"<<year;
    }
private: 
    int day, month, year; 

};
ostream & operator<<(ostream & ostr, const Date & d) {
      d.printOn(ostr);
      return ostr;
}

The error with my person constructor is that there is no default constructor for Date (but I am not supposed to have one)

Thanks!

Recommended Answers

All 3 Replies

You should call the constructor of Date in your initialization list, and you can pass it the three integers you get in the Person constructor:

Person(const char * their_name, const char * email, int aDay, int aMonth, int aYear) : 
       Date(aDay, aMonth, aYear), 
       name(0), email_address(0) {
    name = new char[strlen(their_name) + 1]; 
    strcpy_s(name, strlen(their_name) + 1, their_name); 
    email_address = new char[strlen(email) + 1]; 
    strcpy_s(email_address, strlen(email) + 1, email); 
    cout << "\nPerson(...) CREATING: ";
    printOn(cout);
}

That should work.

Thank you so much!
This is the error with added that:
"Date" is not a nonstatic data member or base class of "Person"

I don't even understand what that means? I didn't state anything was static...

Did you put Date as a base class? Well, I guess I realize now that it doesn't really make much sense for Person to be a special kind of Date.

Please show the full declaration of the class Person.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.