0

我有以下 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 *)'

知道如何编译吗?

4

1 回答 1

1

好的。我明白了,该new行应该是这样的(或new从全局命名空间中获取运算符):

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

显然newCObject 的运算符不支持nothrow_t.

于 2013-08-16T05:13:30.960 回答