不能使用“新”;关于引用类型;使用“gcnew”;反而 ?在VS 2005中
我正在使用 C++ 语言,当我尝试创建 DataTable 的简单对象时,出现一个奇怪的错误,它给出错误
System::Data::DataTable* myDataTable = new DataTable();
即使我尝试过这个 System::Data::DataTable myDataTable = new DataTable(); 出现以下错误请帮忙。
错误 C2750:“System::Data::DataTable”:无法在引用类型上使用“new”;使用“gcnew”代替 错误 C2440:“初始化”:无法从“System::Data::DataTable *”转换为“System::Data::DataTable ^”
I am using a C++ language I am getting a strange error when I am try to create a simple object of DataTable its giving error
System::Data::DataTable* myDataTable = new DataTable();
even I tried this
System::Data::DataTable myDataTable = new DataTable();
getting the following error please help.
error C2750: 'System::Data::DataTable' : cannot use 'new' on the reference type; use 'gcnew' instead
error C2440: 'initializing' : cannot convert from 'System::Data::DataTable *' to 'System::Data::DataTable ^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的语言称为 C++/CLI,而不是普通的 C++。在 C++/CLI 中,您可以访问 .NET 内容,例如
数据表
。语义与原始指针有点不同:“
^
”表示 托管句柄。在底层,它是一个指向 GC 堆上对象的指针。您不能对托管句柄进行指针算术。您不能手动删除
它们。 GC 会处理它们。除非明确固定对象,否则还可以自由移动对象。使用gcnew
在托管堆上分配对象。它返回一个句柄,而不是原始指针。您无法使用new
在非托管 C++ 堆上创建 .NET 引用类型。The language you are using is called C++/CLI, not plain C++. In C++/CLI, you can access .NET stuff like
DataTable
. The semantics are a bit different from raw pointers:"
^
" denotes a managed handle. Under the hood, it's a pointer to an object on the GC heap. You can't do pointer arithmetic on managed handles. You don'tdelete
them manually. The GC will take care of them. It's also free to move the objects around unless they are pinned explicitly.gcnew
is used to allocate objects on the managed heap. It returns a handle, not a raw pointer. You can't create .NET reference types on unmanaged C++ heap usingnew
.