user3054780
user3054780

Reputation: 15

C++ Constructor with other class-data-type

first of all, hi to everyone,

i'm new to this forum, so i hope, i haven't made any rookie-mistakes, if so - please correct me :). Second: English is not my native language, so i hope i can make my problem clear.

Regarding my problem:

I have to write a C++ program with 2 classes, class 1 Date with 3x unsigned int for day, month and year, and class 2 Time with 2x unsigned int for minute and hour,and 1x Date.

I know it's not exactly clear what my problem is, so here's a simplified version of my code: main:

#include "Date.hpp"
#include "Time.hpp" //(+ iomanip / iostream)

int main(){
Date d1; //Calls Default Constructor of class Date, which initialises with (1,1,2000)
Time t1(d1,23,30); //should call ovrld-constr. of class Time and keep the date of d1

in my "Time.cpp" i tried smth like:

Time::Time(Date x, unsigned int y, unsigned int z)
  : d(Date::Date(x)), minute(y), hour(z) //try to call the def constr of class Date
{
return;
} 

Time::Time() : Time(Date::Date(), 00, 00) //should call the ovrld constr of class Time
{
return;
}

but of course it doesn't work...The Compiler always says:

main.cpp:(.text+0x35c):undefined reference to `Time::Time()'

if it helps, here's a extract of my (completely working) "Date.cpp":

Date::Date(unsigned int x, unsigned int y, unsigned int z)
 : day(x), month(y), year(z)
{
if(valiDate(day, month, year)==false){ //ValiDate is a function which checks the input                          
day=1;month=1;year=2000;} //default date, if Date is not Valid
return;
} 

Date::Date() : Date(1,1,2000)
{
return;
}

I hope this was enough to make my problem clear, if not, i can always show you the complete source code. Important note: I'm not (!) allowed to edit the main.cpp. Also, if someone could explain my mistake rather than just posting a complete solution, it would be greatly appreciated, because i think that a solution which I -in a worst case- don't even understand doesn't help me much with my study.

Thanks in advance for the help,

best regards.

edit:

I'm sorry i forgot the two class.hpp.

1)The working Date.hpp (extract)

class Date
{

public:
  Date();
  Date(unsigned int, unsigned int, unsigned int);


private:

  unsigned int day, month, year; 
  bool valiDate(unsigned int, unsigned int, unsigned int);
};

2)my (not working) Time.hpp.

class Time
{

  public:

  Time();
  Time(Date, unsigned int, unsigned int);

  private:

  unsigned int minute, hour;
  Date d;

};

Upvotes: 1

Views: 1211

Answers (1)

derpface
derpface

Reputation: 1691

I reproduced your project and found dozens of compilation and linking errors, but no such error as you've specified. Here's what I did find:

class Time contains a data member of type Date by value, so you need to #include Date.hpp in Time.hpp. If you haven't already done so, you need to add include guards to your headers.

You don't need to explicitly specify the Date constructor in the member initializer of Time, since it's being called anyway:

Time::Time(Date x, unsigned int y, unsigned int z)
  : d(Date::Date(x)), minute(y), hour(z) //try to call the def constr of class Date
{
    return;
} 

can just be:

Time::Time(Date x, unsigned int y, unsigned int z)
    : d(x), minute(y), hour(z)
{

} 

Note that you don't need to write return statements in constructors either. Also note that : d(x) invokes Date's copy constructor, not the default constructor. You haven't defined a copy constructor, and so one is being implicitly created for you to do a shallow copy.

You're also calling one constructor from another:

Time::Time() : Time(Date::Date(), 00, 00)
Date::Date() : Date(1,1,2000)

Delegating constructors is supported in C++11 but not yet supported by all compilers, but you can just initialize the members yourself:

Time::Time() : minute(0),hour(0)
Date::Date() : day(1),month(1),year(2000)

Upvotes: 1

Related Questions