解決DEBUG_NEW : undeclared identifier的問題

來源:互聯網
上載者:User

用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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.