Error when declaring an array without specifying size initially: <error-type> Crew::flightAttendants Incomplete type is not allowed

huangapple go评论71阅读模式
英文:

Error when declaring an array without specifying size initially: <error-type> Crew::flightAttendants Incomplete type is not allowed

问题

给定这个**C++**类...

//C++代码
class Crew {
    Person flightAttendants[]; //错误: &lt;error-type&gt; Crew::flightAttendants 不允许不完整的类型。
    Person captain, firstOfficer;
public:
    Crew(Person, Person, Person);
};

我想声明(但不要在一开始初始化)flightAttendants[] 数组,而不需要事先指定它的长度(我只想在之后指定其大小)。就像Java一样,例如,我们可以这样做:

//JAVA代码
class Lamp {
    private int nLightBulbs;
    private boolean lightBulbs[];
    
    Lamp(int nLightBulbs) {
        this.nLightBulbs = nLightBulbs;
        this.lightBulbs = new boolean[nLightBulbs];
    }
}

这就是问题。

英文:

So given this class in C++...

    //C++ CODE    
        class Crew {
    	    Person flightAttendants[]; //Error: &lt;error-type&gt; Crew::flightAttendants Incomplete type is not allowed.
    	    Person captain, firstOfficer;
        public:
    	    Crew(Person, Person, Person);
        };

I'd like to declare (but NOT at first initialize) the flightAttendants[] array without specifying what would be it's length beforehand (I just want to specify its size after). Just like Java, for example, in which we could do:

    //JAVA CODE    
        class Lamp {
        	private int nLightBulbs;
        	private boolean lightBulbs[];
        
        	Lamp(int nLightBulbs) {
        		this.nLightBulbs = nLightBulbs;
        		this.lightBulbs = new boolean[nLightBulbs];
        	}
        }

That's the question.

答案1

得分: 3

Person flightAttendants[];

你想要的例子是:

 Person * flightAttendants;

然后就像在Java中一样:

this.lightBulbs = new boolean[nLightBulbs];

在C++中执行:

 flightAttendants = new Person[...预期大小...];

但更实际的做法是使用std::vector

 std::vector<Person> flightAttendants;

出于许多原因,包括能够获取其大小/调整大小以及不必管理Person * flightAttendants中使用的指针(即使还有其他安全方式来管理它)


请注意,在Java中,您始终操作实例的指针,在C++中,我们可以选择,而先前的数组/向量不会记住Person实例的指针,而是Person的实例。

英文:

> Person flightAttendants[];

you wanted for instance

 Person * flightAttendants;

then like in Java you have :

> this.lightBulbs = new boolean[nLightBulbs];

in C++ do

 flightAttendants = new Person[...expected size...];

But it is very more practical to use a std::vector

 std::vector&lt;Person&gt; flightAttendants;

for a lot of reasons including to be able to get its size/to resize it, and to not have to manage the pointer used in Person * flightAttendants (even there are other ways to manage it in a secure way)


Note in Java you always manipulate pointer to instances, in C++ we have the choice and the previous array/vector do not memorize pointer to instances of Person but instances of Person

huangapple
  • 本文由 发表于 2020年8月4日 19:53:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63246318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定