重写“新”时的编译错误 [英] Compiling error when overwriting "new"

查看:349
本文介绍了重写“新”时的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想控制mem泄漏在我的代码。在我的一般头文件中,我添加了这个代码:

I'm trying to control mem leaks in my code. In my general header file I've added this code:

    // You may need to locate mem leaks
    #define ZEL_CHECK_MEMORY_LEAKS
    #ifdef ZEL_CHECK_MEMORY_LEAKS
        #define _CRTDBG_MAP_ALLOC
        #include <cstdlib>
        #include <crtdbg.h>

        #ifdef _DEBUG
        #ifndef DBG_NEW
        #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
        #define new DBG_NEW
        #endif
        #endif  // _DEBUG

        #define zelInitMemoryCheck() \
            _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF)
    #else
        #define zelInitMemoryCheck()
    #endif //ZEL_CHECK_MEMORY_LEAKS



如果我添加这个代码,我得到这个编译错误:

If I add this code, I get this compilation error:

1>c:\archivos de programa\microsoft visual studio 9.0\vc\include\xlocmon(283) : error C2061: syntax error : identifier '_DebugHeapTag_func'
1>        c:\archivos de programa\microsoft visual studio 9.0\vc\include\xlocmon(281) : while compiling class template member function 'size_t std::moneypunct<_Elem,_Intl>::_Getcat(const std::locale::facet **,const std::locale *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Intl=true
1>        ]
1>        c:\archivos de programa\microsoft visual studio 9.0\vc\include\xlocmon(908) : see reference to class template instantiation 'std::moneypunct<_Elem,_Intl>' being compiled
1>        with
1>        [
1>            _Elem=char,
1>            _Intl=true
1>        ]

此外,在我的源代码中,我有这个包含:

In addition, In my source code I have this inclusion:

#include "core/zelCoreLib.h"
#include <boost/shared_ptr.hpp>

其中mem泄漏控制代码放在zelCoreLib.h中

Where mem leak control code is into zelCoreLib.h

对于那些没有VC9.0,这是失败的代码

To those not have VC9.0 this is the code that "fails"

static size_t __CLRCALL_OR_CDECL _Getcat(const locale::facet **_Ppf = 0,
        const locale *_Ploc = 0)
        {   // return locale category mask and construct standard facet
        if (_Ppf != 0 && *_Ppf == 0)
            *_Ppf = _NEW_CRT moneypunct<_Elem, _Intl>(
                _Locinfo(_Ploc->name()), 0, true);
        return (_X_MONETARY);
        }

它似乎是字符串和区域设置的一部分。另外,作为一个附加信息,我使用Lua和LuaBind库

It seems to be part of string and locale facilities. Also, as an additional information, I use Lua and LuaBind libs

任何帮助都会受到欢迎

推荐答案

您有 #define new new 是关键字,如果在标准库中包含任何标题,则 #define 会导致未定义的行为。例如,标准库几乎肯定会在某些地方使用展示位置新增功能,而且您的宏会导致任何使用展示位置新功能。它也会导致任何类特定的 new 破坏—和库可能也使用这些。您无法重新定义关键字,并希望任何操作。

You have a #define new. new is a keyword, and #defineing it results in undefined behavior if you include any header in the standard library. The standard library almost certainly uses placement new in some places, for example, and your macro will cause any use of placement new to break. It will also cause any class specific new to break—and the library might use these as well. You cannot redefine keywords and expect anything to work.

这篇关于重写“新”时的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆