C++ 调用不同的类';构造函数中的构造函数?
在单独的C++ 调用不同的类';构造函数中的构造函数?,c++,class,constructor,C++,Class,Constructor,在单独的.h文件中: class Appt{ public: Appt(string location, string individual, DaTime whenwhere); private: string individual; string location; DaTime whenwhere; // I thought this would initialize it
.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;
}
// 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);
}
在单独的.cc
文件中:
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;
}
// 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()
:
我的问题是,是否有一种干净的方法可以在Appt
中调用DaTime
s构造函数?我知道这种方法行不通,因为我的DaTime
,a
实例将在构造函数完成后死亡
编辑:我得到的错误是:
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
成为Appt
的数据成员,并在构造函数初始化列表中初始化它:
Appt::Appt(string location, string individual, DaTime whenwhere) : a (whenwhere)
{
....
}
此外,还不清楚是否要按值传递所有参数。考虑传递<代码> const < /Cord>替代引用。
注意:您收到的错误似乎表明您的类中确实有一个DaTime
数据成员,并且您没有在构造函数初始化列表中初始化它。这意味着必须执行默认初始化,并且由于DaTime
没有默认构造函数,因此会出现错误。记住:一旦进入构造函数主体,所有数据成员都已初始化。如果您不显式地初始化它们,它们将得到默认构造
DaTime whenwhere; // I thought this would initialize it
这不是初始化。这只是一份会员声明<当调用DaTime
构造函数时,将在哪里初始化code>whenwhere
。在C++11中,还可以在声明点进行初始化:
DaTime whenwhere{arg1, arg2, aer3};
在类
Appt
中包括以下代码:
DaTime a;
在Appt的构造函数中
Appt::Appt(string location, string individual, DaTime whenwhere)
{
a_SetLocation(location);
a_SetIndividual(individual);
a = whenwhere; //call copy constructor of `DaTime`
}
关于错误:
您的DaTime
类没有默认构造函数
因此,您可以做的是将DaTime
的值传递给Appt
的构造函数,并创建DaTime
的对象,作为a=new DaTime(a,b,c)
或者通过引用传递对象
希望这能对您有所帮助。但是我必须返回一个
?是的,我有一个DaTime数据成员,所以这很有意义。我以为我已经初始化了它。我不想发布大量代码,但我将添加一个编辑来显示该类的该部分。@AllenS您不需要发布大量代码,只是一个复制问题的简化示例。但应该很容易发现数据成员是否已初始化。您的编译器错误很明显:在某些情况下需要默认构造函数,这意味着您没有以应有的方式初始化对象。@AllenS因此现在您有两个DaTime
数据成员,a
和whenwhere
。您必须正确地初始化它们。如果我正在将a
初始化为whenwhere
,那么我应该在哪里初始化whenwhere
呢?谢谢您的帮助。这并不能解决我的错误,尽管与我试图解决的问题相比,这是有意义的。我进行了编辑以包含我的错误,因为我的错误可能在别处。@AllenS我的解决方案修复了该错误。我将添加一个注释来解释原因。