gpt4 book ai didi

c++ - 在构造函数中调用不同类的构造函数?

转载 作者:行者123 更新时间:2023-11-28 02:55:58 25 4
gpt4 key购买 nike

<分区>

在单独的 .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);

我的问题是,是否有一种干净的方法让我在 Appt 中调用 DaTime 的构造函数?我知道这种方式行不通,因为我的 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&)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com