How do I make a vector/array of System::Windows::F

2019-05-31 03:25发布

问题:

Couldn't find any answer to this problem, or not even any questions asked. So what I'm trying to do, is a std::vector, maybe just a normal array, of Checkboxes.

std::vector< System::Windows::Forms::CheckBox^ >m_items;
m_items.push_back( myCheckbox );

That's what I currently have, and it clearly ain't working. So does anyone have any ideas, on how to get it working, cause I've tried everything I can, but vectors don't seem to support Checkboxes.

Incase you need the error code:

c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory(200): error C3699: '&&' : cannot use this indirection on type 'System::Windows::Forms::CheckBox ^'
1>          c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(421) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=System::Windows::Forms::CheckBox ^
1>          ]
1>          c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(481) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
1>          with
1>          [
1>              _Ty=System::Windows::Forms::CheckBox ^,
1>              _Alloc=std::allocator<System::Windows::Forms::CheckBox ^>
1>          ]
1>          d:\programming\vc++ projects\mahi wcs race maker\mahi wcs race maker\RestrictedItemsForm.h(69) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=System::Windows::Forms::CheckBox ^
1>          ]
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(630): error C3699: '&&' : cannot use this indirection on type 'System::Windows::Forms::CheckBox ^'
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(655): error C3699: '&&' : cannot use this indirection on type 'System::Windows::Forms::CheckBox ^'
1>d:\programming\vc++ projects\mahi wcs race maker\mahi wcs race maker\RestrictedItemsForm.h(69): error C4368: cannot define 'm_items' as a member of managed 'MahiWCSRaceMaker::RestrictedItemsForm': mixed types are not supported
1>d:\programming\vc++ projects\mahi wcs race maker\mahi wcs race maker\RestrictedItemsForm.h(170): error C2663: 'std::vector<_Ty>::push_back' : 2 overloads have no legal conversion for 'this' pointer

回答1:

The regular std::vector does not support CLR reference types. Instead, you must use cliext::vector. Include the following:

#include <cliext/vector>

And use with:

cliext::vector<System::Windows::Forms::CheckBox^> items;
items.push_back(myCheckBox);

Of course, you can also use the regular .Net collections, like List<T>, which behaves similarly as a vector.