我有以下MFC(C ++)代码,该代码将指针分配给数组,而在内存不足的情况下无需引发异常。我用Visual Studio 2008编译它。

struct MY_ITEM_INFO
{
    CString str;
    int n;

    MY_ITEM_INFO()
    {
        n = 0;
    }
};

CArray<MY_ITEM_INFO>* pArrResItems = new (std::nothrow) CArray<MY_ITEM_INFO>();
if(pArrResItems != NULL)
{
    //Got it!

    //Remove it
    delete pArrResItems;
}


当我尝试对其进行编译时,这在new运算符行上给出了以下错误消息:

error C2665: 'CObject::operator new' : none of the 3 overloads could convert all the argument types
could be 'void *CObject::operator new(size_t,void *)'


任何想法如何使其编译?

最佳答案

好。我知道了,new行应该是这个(或者从全局名称空间中获取new运算符):

CArray<MY_ITEM_INFO>* pArrResItems = ::new (std::nothrow) CArray<MY_ITEM_INFO>();


显然,CObject的new运算符不支持nothrow_t

10-07 19:45
查看更多