0

I have a struct Arguments that i use for giving a bunch of variables easily to functions.

struct Arguments
{
    vector<Body> boxes[30][30];
}

Boxes is a 2d array, with a vector inside of it i think. I have given the array vectors with different values, but later in my code i want to completly empty the 2d array.

I have tried a few different things, but i am completly new to the c++ language and dont know how i should do it.

void func(Arguments &args)
{
    args.boxes = new vector<Body>[30][30];
}

This gives me a compile error that i have incompatible types "error: incompatible types in assignment of 'std::vector*' to 'std::vector [30][30]"

How can i fix this? Any help would be great!

  • You have a 2D array holding 900 separate vectors. Did you really want 900 vectors, or do you really want just 1 vector that can hold 900 boxes? I think you don't understand the difference between a vector and an array. In any case, you are getting the error because the original array can't be assigned a new array, and your use of `new` is completely wrong anyway – Remy Lebeau Mar 11 '23 at 20:56
  • I could use i vector that can hold 900 boxes, but seeing as a box is a vector, wont it be the same as one 30 by 30 vector? – Øystein Bringsli Mar 11 '23 at 20:58
  • 2
    Change `vector boxes[30][30];` to simply `vector boxes;` You don't need to specify sizes at **compile-time**, since `vector` holds a *dynamic* array that grows and shrinks at **runtime** as needed. I suggest you get some [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), they cover this in detail. – Remy Lebeau Mar 11 '23 at 21:01

0 Answers0