错误 C2661:'CObject::operator new':没有重载函数需要 4 个参数

error C2661: 'CObject::operator new' : no overloaded function takes 4 arguments

本文关键字:函数 重载 参数 new CObject C2661 operator 错误      更新时间:2023-10-16

我有一个内存泄漏,我试图在我的 mfc 程序中寻找它。通常我会做如下事情:

头文件

// Leak Detection
#if defined(WIN32) && defined(_DEBUG)
     #define _CRTDBG_MAP_ALLOC
     #include <stdlib.h>
     #include <crtdbg.h>
#endif

CPP 文件

// Leak detection
#if defined(WIN32) && defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
    #ifdef DEBUG_NEW 
        #undef DEBUG_NEW
    #endif
    #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
    #define new DEBUG_NEW
#endif

这种技术在大多数文件中都运行良好,但是当我将其包含在某些文件(例如我的文档)中时,出现错误:错误 C2661:"CObject::运算符新":没有重载函数需要 4 个参数

这里的解决方案是什么?我应该在某个地方或某事上 #undef 新事物吗?

谢谢!

我也使用与您相同的功能进行泄漏检测。

您可以注释掉或删除DEBUG_NEW定义块,假设您不再需要它来捕获内存泄漏。或者,如果您仍然需要它,请保持原样并使用

#ifdef _DEBUG
#undef new
    CMyOject* pMyObjectInst = new CMyObject();
#define new DBG_NEW
#endif  

因此,您在创建对象之前取消定义 new(请参阅错误列表中的行号),并在之后立即再次重新定义它,以便在此对象创建之后发生的任何内存泄漏仍然可以识别。

我有类似的问题,#define new DEBUG_NEW放在文件中#include ...语句之前.cpp。更改顺序解决了我的问题。