解决DEBUG_NEW : undeclared identifier的问题

用VC++的向导生成的类, 都带有下面这几行:

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

有时在使用new操作符的时候就会提示错误error C2065: 'DEBUG_NEW ': undeclared identifier, 特别是在ATL/WTL中新定义类的时候, 在DEBUG版中出现, RELEASE中没有问题.

解决办法是两个:

一. 直接将这几行注释掉

二. 在#define new DEBUG_NEW 之前定义:
 #define DEBUG_NEW new(THIS_FILE, __LINE__)

原因分析:

stdafx.h包含afxwin.h,afxwin.h又包含了afx.h, afx.h文件中有如下定义:
1. #if defined(_DEBUG) && !defined(_AFX_NO_DEBUG_CRT)
2.
3. // Memory tracking allocation
4. void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
5. #define DEBUG_NEW new(THIS_FILE, __LINE__)
6. #if _MSC_VER > = 1200
7. void AFX_CDECL operator delete(void* p, LPCSTR lpszFileName, int nLine);
8. #endif

在第5行, 就出现了#define DEBUG_NEW new(THIS_FILE, __LINE__) , 所以, 如果是MFC程序, afxwin.h和afx.h都是包含了的, 但如果是ATL程序, 这两个文件是没有的, 故必然出错.

那么有时在MFC程序中也出现这种错误是怎么回事呢?

有时你禁止预编译头stdafx, 系统找不到这个定义,就会报错, 从而出现错误error C2065: 'DEBUG_NEW' : undeclared identifier


你可能感兴趣的:(c,File,delete,mfc,Allocation)