最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】error C2750: ‘System::Windows::Forms::OpenFileDialog’ : cannot use ‘new’ on the reference type; use ‘gcnew’ instead XXXhelloworld_vcForm1.h 98

工作和技术 crifan 1459浏览 0评论

【已解决】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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
79 queries in 0.147 seconds, using 22.11MB memory