Can someone explain why the following code will not compile (formatted weirdly to make it easier to view):
ListView ^ listview = gcnew ListView(); listview->Items->AddRange( gcnew array<ListViewItem^> { gcnew ListViewItem( gcnew array<String^> { L"red", L"fish" } ), gcnew ListViewItem( gcnew array<String^> { L"green", L"eggs" } ) });
This gives a compilation error
error C2440: 'initializing': cannot be converted from 'const wchar_t [4]' to 'System :: Windows :: Forms :: ListViewItem ^'
If the code is split into two lines as follows, then everything is fine:
ListView^ listview = gcnew ListView(); ListViewItem^ lvi1 = gcnew ListViewItem( gcnew array<String^> { L"red", L"fish" } ); ListViewItem^ lvi2 = gcnew ListViewItem( gcnew array<String^> { L"green", L"eggs" } ); listview->Items->AddRange( gcnew array<ListViewItem^> { lvi1, lvi2 });
Ignoring why someone wants to make a monolithic single-line layer to populate the ListView, why does the compiler have problems initializing ListViewItems in the source code, and how will such one liner be written?
compiler-construction c ++ - cli
Waldo
source share