错误 C2660: Gdiplus::GdiplusBase::operator new: 函数不带三个参数

    技术2025-09-23  45

    在使用 GDI + 的 Microsoft 基础类 (MFC) 应用程序的调试版本时, 可能会出现类似于以下内容的错误消息: 错误 C2660: Gdiplus::GdiplusBase::operator new: 函数不带三个参数

     

          原因:在调试版本中 MFC 定义扩展到带有两个额外的参数的重载 new 运算符的 new 运算符的预处理器宏。     

    MFC 可以使用此信息对程...在调试版本中 MFC 定义扩展到带有两个额外的参数的重载,额外的参数是源文件名称和代码行号。MFC 可以使用此信息对程序员在调试模式下时报告内存泄漏。这适用于 MFC 类因为 MFC 提供的 new 接受额外的参数的重载的不同而不同。

    但是,此扩展由预处理器来完成,因为它会影响所有使用 new 运算符。如果在项目中使用了任何非 MFC 类,其 new 运算符是也即使没有合适的重载,new 的可用在该类中扩展。这是发生在 GDI + 中,如此一来,您收到一个编译时错误消息。

     

    要变通解决此问题,选择下列方法之一: 1、通过注释掉以下行在源文件中的代码关闭预处理器扩展: #ifdef _DEBUG #define new DEBUG_NEW #endif     

    注: 此方法具有不使用的缺点在 MFC 中功能的信息,这些功能可帮助您跟踪内存分配和泄漏。

    2、GDI + 使用重载为提供 new 的和 delete 运算符通过编写一些代码,可接受,并丢弃其他参数。您可以将下面的代码演示了这种方法,粘贴到一个新的头文件,并包括新的头文件,而不是 Gdiplus.h 文件 Ensure that GdiPlus header files work properly with MFC DEBUG_NEW and STL header files.

    #define iterator _iterator

    #ifdef _DEBUG

    namespace Gdiplus { namespace DllExports {    #include <GdiplusMem.h> };

    #ifndef _GDIPLUSBASE_H #define _GDIPLUSBASE_H class GdiplusBase {    public:     void (operator delete)(void* in_pVoid)     {      DllExports::GdipFree(in_pVoid);     }

        void* (operator new)(size_t in_size)     {      return DllExports::GdipAlloc(in_size);     }

        void (operator delete[])(void* in_pVoid)     {      DllExports::GdipFree(in_pVoid);     }

        void* (operator new[])(size_t in_size)     {      return DllExports::GdipAlloc(in_size);     }

        void * (operator new)(size_t nSize, LPCSTR lpszFileName, int nLine)     {      return DllExports::GdipAlloc(nSize);     }

        void operator delete(void* p, LPCSTR lpszFileName, int nLine)     {      DllExports::GdipFree(p);     }

       }; #endif // #ifndef _GDIPLUSBASE_H } #endif // #ifdef _DEBUG

    #include <gdiplus.h> #undef iterator Ensure that Gdiplus.lib is linked. #pragma comment(lib, "gdiplus.lib")

     

    微软官方原文地址如下: http://support.microsoft.com/kb/317799/

    最新回复(0)