內單獨.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數據成員,所以這是有道理的。我以爲我已經初始化了它。我討厭發佈大量的代碼,但我會添加一個編輯來顯示該部分的類。 –