在列表框中显示向量元素 [英] show vector elements in listbox
问题描述
我想在列表框中的向量中显示元素。但是,我不断得到一个错误:错误C2664:'System :: Windows :: Forms :: ListBox :: ObjectCollection :: Add':不能转换参数1从'std :: basic_string< _Elem,_Traits,_Alloc>到'System :: Object ^'
I would like to display the elements in a vector in a listbox. However, I'm constantly getting a error: error C2664: 'System::Windows::Forms::ListBox::ObjectCollection::Add' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Alloc>' to 'System::Object ^'
我在c ++ / cli中使用windows窗体。
这是代码:
I am using windows form in c++/cli. this is the code:
for (size_t z = 0; z < container.size(); z++){
listBox_name->Items->Add(container[z]);
}
推荐答案
您的向量是 std :: string
的向量。使用 marshal_as
将 std :: string
转换为托管 String ^
托管列表框可以接受。
Based on the error message, your vector is a vector of std::string
. Use marshal_as
to convert the std::string
to a managed String^
that the managed list box can accept.
for (size_t z = 0; z < container.size(); z++){
listBox_name->Items->Add(marshal_as<String^>(container[z]));
}
如果你发现你做的很多, std :: string
为完全管理类型,例如 List< String ^> ^
。
If you find you're doing this a lot, consider changing your vector of std::string
to be a fully-managed type, such as List<String^>^
.
这篇关于在列表框中显示向量元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!