2

So, I am creating an object that have an array as it's instance. The size of this array will determined by the client program. Later in my program, I have to create a temp array that have the same capacity as the instance variable. So, I put:

int temp[capacity];

However, when I try to compile it, it failed. It said that I have to have a fix value instead of putting capacity. Any idea how can I fix this problem? thx

4

2 に答える 2

4

capacityコンパイル時に既知の場合にのみ、そのような配列を作成できます。動的サイズの配列の場合は、次を使用しますstd::vector

#include <vector>

std::vector<int> temp(capacity); // makes a vector with capacity elements
于 2013-01-21T00:38:21.083 に答える
-1

これを書く代わりに:

int temp[capacity]

書くだけ:

int* temp = (int*)malloc(capacity);
于 2013-11-22T19:46:35.157 に答える