解决DEBUG_NEW : undeclared identifier的问题

本文介绍了解决VC++环境下使用new操作符时出现的DEBUG_NEW未定义标识符错误的方法,包括直接注释掉预处理指令或重新定义DEBUG_NEW,并分析了问题产生的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用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
【天涯博客】本文地址http://blog.tianya.cn/blogger/post_show.asp?BlogID=1970966&PostID=16659926

--------------------Configuration: My - Win32 Debug-------------------- Compiling resources... Compiling... StdAfx.cpp Compiling... My.cpp MainFrm.cpp MyDoc.cpp MyView.cpp F:\My\MyView.cpp(59) : error C2065: 'dotMatrix_Liu' : undeclared identifier F:\My\MyView.cpp(59) : error C2109: subscript requires array or pointer type F:\My\MyView.cpp(59) : error C2109: subscript requires array or pointer type F:\My\MyView.cpp(108) : error C2039: 'DrawVectorName' : is not a member of 'CMyView' f:\my\myview.h(13) : see declaration of 'CMyView' F:\My\MyView.cpp(116) : error C2084: function 'const struct AFX_MSGMAP *__stdcall CMyView::_GetBaseMessageMap(void)' already has a body F:\My\MyView.cpp(116) : error C2084: function 'const struct AFX_MSGMAP *__thiscall CMyView::GetMessageMap(void) const' already has a body F:\My\MyView.cpp(116) : error C2374: 'protected: static struct AFX_MSGMAP const CMyView::messageMap' : redefinition; multiple initialization f:\my\myview.h(54) : see declaration of 'protected: static struct AFX_MSGMAP const CMyView::messageMap' F:\My\MyView.cpp(116) : error C2374: 'private: static struct AFX_MSGMAP_ENTRY const * const CMyView::_messageEntries' : redefinition; multiple initialization f:\my\myview.h(54) : see declaration of 'private: static struct AFX_MSGMAP_ENTRY const * const CMyView::_messageEntries' F:\My\MyView.cpp(118) : error C2509: 'OnKeyDown' : member function not declared in 'CMyView' f:\my\myview.h(13) : see declaration of 'CMyView' F:\My\MyView.cpp(123) : error C2040: 'dotMatrix_Liu' : 'unsigned char [16][16]' differs in levels of indirection from 'int' Generating Code... 执行 cl.exe 时出错. My.exe - 1 error(s), 0 warning(s) 怎么解决
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值