关于宏与头文件错误的冲突问题的解决

本文探讨了宏与头文件冲突的问题,特别是在重新定义new操作符时遇到的编译错误。通过调整包含顺序和宏定义的位置,成功解决了编译过程中出现的语法错误。

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

关于宏与头文件错误的冲突问题的解决
宏与头文件错误的冲突问题
程序如下:
// SisSocket.cpp : implementation file
//
#include "stdafx.h"
#include "DataServer.h"
#include "SisSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "DataServerDlg.h"
//...其他代码
// DataServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DataServer.h"
#include "DataServerDlg.h"
#include "EzDnaApi.h"
#include "EzDnaServApi.h"
#include "SafeArrayHelper.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//...其他代码
编译错误如:
c:/program files/microsoft visual studio/vc98/include/new(35) : error C2061: syntax error :
identifier 'THIS_FILE'
c:/program files/microsoft visual studio/vc98/include/new(35) : error C2091: function
returns function
c:/program files/microsoft visual studio/vc98/include/new(35) : error C2809: 'operator new'
has no formal parameters
等类似错误
双击错误
inline void *__cdecl operator new(size_t, void *_P)
{return (_P);
经指点,将
// SisSocket.cpp : implementation file
//
#include "stdafx.h"
#include "DataServer.h"
#include "SisSocket.h"
#include "DataServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//...其他代码
问题解决,出在new重定义上
注:THIS_FILE为static char[],在debug时内容为本文件路径,供输出错误信息使用。
//-----------------------------------------------------------------------
在文件test7Dlg.cpp当有如下定义时:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <vector>
就会出现如下错误:
c:/program files/microsoft visual studio/vc98/include/new(35) : error C2061: syntax error : identifier 'THIS_FILE'
c:/program files/microsoft visual studio/vc98/include/new(35) : error C2091: function returns function
c:/program files/microsoft visual studio/vc98/include/new(35) : error C2809: 'operator new' has no formal parameters
c:/program files/microsoft visual studio/vc98/include/new(36) : error C2061: syntax error : identifier 'THIS_FILE'
c:/program files/microsoft visual studio/vc98/include/new(37) : error C2091: function returns function
c:/program files/microsoft visual studio/vc98/include/new(37) : error C2556: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,const struct std::nothrow_t &)' : overloaded function differs only by return type from 'void *(__cdecl *__cdecl op
erator new(void))(unsigned int)'
        c:/program files/microsoft visual studio/vc98/include/new(35) : see declaration of 'new'
c:/program files/microsoft visual studio/vc98/include/new(41) : error C2061: syntax error : identifier 'THIS_FILE'
c:/program files/microsoft visual studio/vc98/include/new(42) : error C2091: function returns function
c:/program files/microsoft visual studio/vc98/include/new(42) : error C2556: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,void *)' : overloaded function differs only by return type from 'void *(__cdecl *__cdecl operator new(void))(unsig
ned int)'
        c:/program files/microsoft visual studio/vc98/include/new(35) : see declaration of 'new'
c:/program files/microsoft visual studio/vc98/include/new(42) : error C2809: 'operator new' has no formal parameters
c:/program files/microsoft visual studio/vc98/include/new(42) : error C2065: '_P' : undeclared identifier
c:/program files/microsoft visual studio/vc98/include/memory(16) : error C2061: syntax error : identifier 'THIS_FILE'
c:/program files/microsoft visual studio/vc98/include/memory(17) : error C2091: function returns function
c:/program files/microsoft visual studio/vc98/include/memory(17) : error C2784: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,class std::allocator<`template-parameter257'> &)' : could not deduce template argument for 'void *(__cdecl *)(u
nsigned int,class std::allocator<_Ty> &)' from 'void *(__cdecl *)(unsigned int)'
c:/program files/microsoft visual studio/vc98/include/memory(17) : error C2785: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,class std::allocator<`template-parameter257'> &)' and 'void *(__cdecl *__cdecl operator new(void))(unsigned int
)' have different return types
        c:/program files/microsoft visual studio/vc98/include/memory(16) : see declaration of 'new'
c:/program files/microsoft visual studio/vc98/include/memory(17) : error C2809: 'operator new' has no formal parameters
c:/program files/microsoft visual studio/vc98/include/memory(20) : error C2954: template definitions cannot nest
Error executing cl.exe.
解决办法:
 1。把#include <vector>语句放在test7Dlg.h文件中
 2。把定义顺序颠倒下,如下所示:
#include <vector>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
原因:
 VC6是可以支持的,不过头文件的声明
 #include <list>
 using namespace std;
 要在系统生成的代码
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #undef THIS_FILE
 static char THIS_FILE[] = __FILE__;
 #endif
 之前 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值