内单独.h
文件:在构造函数中调用不同类的构造函数?
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;
}
内单独.cc
文件:
// 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);
}
内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);
我的问题是,如果有,我叫DaTime
一个干净的方式在Appt
里面的构造函数?我知道这种方式是行不通的,因为DaTime
,a
的实例会在constructor
完成后死亡。
编辑:我得到的错误是:
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&)
但是,我将不得不返回'a'? –
@AllenS Ehm,no。 – juanchopanza
是的,我确实有DaTime数据成员,所以这是有道理的。我以为我已经初始化了它。我讨厌发布大量的代码,但我会添加一个编辑来显示该部分的类。 –