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
You can only construct such an array if capacity
is known at compile time. For dynamically sized arrays, use std::vector
:
#include <vector>
std::vector<int> temp(capacity); // makes a vector with capacity elements
Instead of writing this:
int temp[capacity]
Just write:
int* temp = (int*)malloc(capacity);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.