[英]Incompatible types error when inheriting without specifying generic type of parent class
[英]Error when declaring an array without specifying size initially: <error-type> Crew::flightAttendants Incomplete type is not allowed
所以在 C++ 中给出这个class ...
//C++ CODE
class Crew {
Person flightAttendants[]; //Error: <error-type> Crew::flightAttendants Incomplete type is not allowed.
Person captain, firstOfficer;
public:
Crew(Person, Person, Person);
};
我想声明(但不是首先初始化) flightAttendants[]
数组,而不事先指定它的长度(我只想在之后指定它的大小)。 就像Java一样,例如,我们可以这样做:
//JAVA CODE
class Lamp {
private int nLightBulbs;
private boolean lightBulbs[];
Lamp(int nLightBulbs) {
this.nLightBulbs = nLightBulbs;
this.lightBulbs = new boolean[nLightBulbs];
}
}
这就是问题所在。
Person flightAttendants[];
例如你想要
Person * flightAttendants;
然后像 Java 你有:
this.lightBulbs = new boolean[nLightBulbs];
在 C++ 做
flightAttendants = new Person[...expected size...];
但是使用std::vector
更实用
std::vector<Person> flightAttendants;
出于很多原因,包括能够获得它的大小/调整它的大小,并且不必管理Person * flightAttendants
中使用的指针(即使有其他方法可以安全地管理它)
请注意,在 Java 中,您总是操作指向实例的指针,在 C++ 中,我们可以选择,并且之前的数组/向量不记忆指向Person 实例的指针,而是 Person实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.