【已解决】error C2750: ‘System::Windows::Forms::OpenFileDialog’ : cannot use ‘new’ on the reference type; use ‘gcnew’ instead XXXhelloworld_vcForm1.h 98
【问题背景】
VS2005下,打算在一个button1的click函数中添加一个OpenFileDialog,添加这样的代码:
OpenFileDialog ofdOpenMp3 = new OpenFileDialog();
结果编译出错:
error C2750: ‘System::Windows::Forms::OpenFileDialog’ : cannot use ‘new’ on the reference type; use ‘gcnew’ instead XXXprojectshelloworld_vchelloworld_vcForm1.h 98
【解决办法】
按照提示,去用gcnew替代new,变成这样:
OpenFileDialog^ ofdOpenMp3 = gcnew OpenFileDialog;
对应的VS2005的help中的解释:
gcnew creates an instance of a managed type (reference or value type) on the garbage collected heap. The result of the evaluation of a gcnew expression is a handle (^) to the type being created.
具体的意思就是,用gcnew在managed heap上新建一个对象,对应的^abc中的abc不是对象,而是相当于对象的指针。
所以后续引用对象中的成员,不用点”.”,而是用箭头“->”,如下:
ofdOpenMp3->Title = “Open a Mp3 File”;
转载请注明:在路上 » 【已解决】error C2750: ‘System::Windows::Forms::OpenFileDialog’ : cannot use ‘new’ on the reference type; use ‘gcnew’ instead XXXhelloworld_vcForm1.h 98