Wal*_*ldo 6 compiler-construction c++-cli
有人可以解释为什么以下代码将无法编译(格式奇怪,使其更容易看到问题):
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" } ) 
});
这给出了编译错误
错误C2440:'初始化':无法从'const wchar_t [4]'转换为'System :: Windows :: Forms :: ListViewItem ^'
如果代码分为两行,如下所示,那么一切都很好:
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 
});
忽略为什么有人想制作一个单片的单行来填充ListView,为什么编译器在原始代码中设置ListViewItems有困难,以及如何编写这样的单行程序?
这就像编译器解析器错误一样大声嘎嘎叫。如果将字符串数组初始值设定项保留为空,则会变得更有趣。然后您将在“输出”窗口中看到以下描述:
1>c:\projects\cpptemp26\Form1.h(77) : error C2552: '$S4' : non-aggregates cannot be initialized with initializer list
1>        'System::Windows::Forms::ListViewItem ^' is not an array or class : Types which are not array or class types are not aggregate
1>c:\projects\cpptemp26\Form1.h(78) : error C2440: 'initializing' : cannot convert from 'const wchar_t [6]' to 'System::Windows::Forms::ListViewItem ^'
1>        Reason: cannot convert from 'const wchar_t *' to 'System::Windows::Forms::ListViewItem ^'
1>        No user-defined-conversion operator available, or
1>        Cannot convert an unmanaged type to a managed type
请注意消息“ListViemItem^ 不是数组或类”。这强烈表明编译器正在将初始化程序应用于 ListViewItem 而不是字符串数组,这是无意义的。它从那里爆炸。
如果有的话,这个问题不会很快得到解决。你知道这个糟糕的解决方法。您可以发帖到 connect.microsoft.com 寻求第二意见。