0

Inside separate .h files:

    class Appt{
        public:
        Appt(string location, string individual, DaTime whenwhere);

        private:
        string individual;
        string location;
        DaTime whenwhere;  // I thought this would initialize it 
        DaTime a;
    }

    class DaTime{
        public:
        DaTime(Day day, Time start, Time end); // Day is enum, Time is class

        private:
        int duration;
        Day day;
        Time start;
        Time end; 

    }

Inside separate .cc files:

    // class Appt constructor
    Appt::Appt(string location, string individual, DaTime whenwhere)
    {
            a_SetLocation(location);
            a_SetIndividual(individual);
            DaTime a(whenwhere); // THE LINE IN QUESTION
    }

    // class DaTime constructor
    DaTime::DaTime(Day day, Time start, Time end)
    {

            dt_SetDay(day);
            dt_SetStart(start);
            dt_SetEnd(end);
    }

inside main():

    /* Creating random variables for use in our classes */
    string loc1 = "B";
    string p1 = "A";

    /* Creating instances of our classes */
    Time t1(8, 5), t2(8, 30);
    DaTime dt1('m', t1, t2);
    Appt A1(loc1, p1, dt1);

My question is if there is a clean way for me to call DaTimes constructor inside of Appt? I know this way wouldn't work because my instance of DaTime, a would die after the constructor was finished.

EDIT: The error I am getting is:

    In constructor ‘Appt::Appt(std::string, std::string, DaTime)’:
    appt.cc: error: no matching function for call to ‘DaTime::DaTime()’
     Appt::Appt(string location, string individual, DaTime when where)
    In file included from appt.h:15:0,
             from appt.cc:15:
    class.h:note: DaTime::DaTime(Day, Time, Time)
      DaTime(Day day, Time start, Time end);
      ^
    note:   candidate expects 3 arguments, 0 provided
    note: DaTime::DaTime(const DaTime&)
Allen S
  • 53
  • 2
  • 11

2 Answers2

2

Make a a data member of Appt and initialize it in the constructor initialization list:

Appt::Appt(string location, string individual, DaTime whenwhere) : a (whenwhere)
{
  ....
}

Also, it isn't clear whether you want to pass all of your parameters by value. Consider passing const references instead.

Note: The error you are getting seems to indicate that you do have a DaTime data member in your class, and that you are not initializing it in the constructor initialization list. This means that a default initialization must be performed, and since DaTime does not have a default constructor, you get the error. Remember: once you are in the body of a constructor, all your data members have been initialized. If you don't initialize them explicitly, they get default constructed.

DaTime whenwhere;  // I thought this would initialize it 

This is not an initialization. It just a member declaration. whenwhere will get initialized when a DaTime constructor is called. In C++11, you can also initialize at the point of declaration:

DaTime whenwhere{arg1, arg2, aer3};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • But then I would have to return `a`? – Allen S Feb 24 '14 at 07:12
  • Yes, I do have a DaTime data member, so that makes sense. I thought I had initialized it. I hate to post a ton of code, but I'll add an edit to show that portion of the class. – Allen S Feb 24 '14 at 07:33
  • @AllenS You don't need to post a ton of code, just a simplified example that reproduces the problem. But it should be easy to spot if a data member has been initialized or not. Your compiler error is pretty clear: the default constructor is required at some point, which means you are not initializing an object in the way it should be. – juanchopanza Feb 24 '14 at 07:40
  • @AllenS So now you have two `DaTime` data members now, `a` and `whenwhere`. You have to initialize *both* of them correctly. – juanchopanza Feb 24 '14 at 07:57
  • If I am initializing `a` to `whenwhere`, where am I supposed to initialize `whenwhere` to? – Allen S Feb 24 '14 at 08:14
  • @AllenS That is up to you to decide. It depends on what you want your program to do. But note that you have two things called `whenwhere`, so your question is not clear. – juanchopanza Feb 24 '14 at 08:27
0

In class Appt include following code:

DaTime a;

and In Constructor of Appt

Appt::Appt(string location, string individual, DaTime whenwhere)
    {
            a_SetLocation(location);
            a_SetIndividual(individual);
            a = whenwhere; //call copy constructor of `DaTime`
    }

About error:

You do not have a default constructor for your DaTime Class.

So, What you can do is pass the values for DaTime ti your constructor of Appt and create an object of DaTime as a= new DaTime(a,b,c).

Or Do pass the object by reference.

Hope this will help you.

A B
  • 1,461
  • 2
  • 19
  • 54
  • Thanks for the help. It doesn't solve my error, though it makes sense compared to my attempt at a solution. I made an edit to include my error since perhaps my error is elsewhere. – Allen S Feb 24 '14 at 07:25
  • @AllenS My solution fixes that error. I will add a note explaining why. – juanchopanza Feb 24 '14 at 07:28