CArray的new运算符导致错误

时间:2013-08-16 03:15:46

标签: c++ windows mfc new-operator

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

知道如何编译吗?

1 个答案:

答案 0 :(得分:1)

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

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

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