Search code examples
winformsvectorc++-clicontainerspicturebox

Why can't I store a PictureBox^ in a std::vector?


For a collision system that I'm building I need to have a vector of images:

vector<PictureBox^> Wall{ Player1,Wall_1,Wall_2 };

This is how I have instantiated the vector, this does not compile successfully.

vector<PictureBox^> Wall;

You can instantiate the vector like this perfectly fine since nothing is being added to it.

And when I try to compile the program with the first instantiation these are some of the errors I get... enter image description here

I have tried pretty much all standard compliant containers such as list and deque, but to no prevail.

Any ideas?


Solution

  • You cannot mix standard C++ with C++/CLI, as their types have special requirements (like their pointers are garbage collected). You'll have to use their specific containers if you want to store gc-ed pointers in it.

    For example, a general purpose array is List:

    List<PictureBox^>^ list = gcnew List<PictureBox^>();