此前的项目没有有意识地对警告进行处理,因此编译时经常报出大量的警告.
除了不清爽的感觉外,无视警告可能遗漏了对改进程序有价值的线索。
本文列举了所遇到的几乎全部的警告.
对每种错误提供以下信息:示例错误信息(黑色斜体表示),处理方式(红色)以及说明.
C4005
warning C4005: 'DEBUG_LOG' : macro redefinition command-line arguments : see previous definition of 'DEBUG_LOG'
hotfox工程C++|Preprocessor|Preprocessor Definitions:去掉DEBUG_LOG
C4018
warning C4018: '<' : signed/unsigned mismatch统一变量类型声明或者强制转换
C4099
warning C4099: 'CScriptData' : type name first seen using 'struct' now seen using 'class'
struct CScriptData;
friend class CScriptData;
修改为friend struct CScriptData;
C4101
warning C4101: 'szPPort' : unreferenced local variable(1)通常删除未使用的变量声明.
(2)如果可能恢复被注释的使用了此变量的代码,可以屏蔽此警告
#pragma warning(push)
#pragma warning(disable:4101)
...///< 报C4101的函数
#pragma warning(pop)
C4117
warning C4117: macro name '__FUNCTION__' is reserved, '#define' ignored
xtrace.h中定义__FUNCTION__.
xtrace是为了在运行时崩溃输出调用栈的第三方代码,早已废弃不用.
__FUNCTION__是vc的扩展关键字,应该是在vc 6.0之后出现的.所以,在vs2005下编译报此错误.
删除所有#include "xtrace.h"的代码.
C4150
warning C4150: deletion of pointer to incomplete type 'ddns::CRuleScheduler'; no destructor calledclass CRuleScheduler;
class CRule {
...
virtual ~CRule() {
if (scheduler_) delete scheduler_;
}
}
这是由于CRuleScheduler是向前引用声明.
把~CRule代码移到cpp文件中.
C4244
warning C4244: 'argument' : conversion from 'time_t' to 'long', possible loss of datatypedef __time64_t time_t;
ltoa替换为_i64toa.
这种警告也帮助发现了类似下面的逻辑错误:
*(unsigned short*)base = htons(*(unsigned short*)p);
正确的应该是:
*(unsigned short*)base = htonl(*(unsigned short*)p);
C4251
warning C4251: 'CRecordsetHandle::m_pRecordset' : class '_com_ptr_t<_IIID>' needs to have dll-interface to be used by clients of class 'CRecordsetHandle'DBHandle.h
class LS_API CRecordsetHandle
{
protected:
_RecordsetPtr m_pRecordset;
};
/// 增加下行代码
template class LS_API _com_ptr_t<_com_IIID<_Recordset,&__uuidof(_Recordset)> >;
umx.h
#ifdef UMX_DLL
UMX_EXPIMP_TEMPLATE template class UMX_DLL_API std::allocator<CDataBlock*>; ///< 增加此行
UMX_EXPIMP_TEMPLATE template class UMX_DLL_API std::vector<CDataBlock*>;
#endif
C4267
warning C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of data
关闭编译选项/Wp64
C4290
warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)#pragma warning( disable : 4290 )
C4297
warning C4297: 'FindClausePos' : function assumed not to throw an exception but doesstatic int FindClausePos(const char *buf,const char *ref) throw()
修改为
static int FindClausePos(const char *buf,const char *ref)
throw()形式的异常声明表示不抛出异常,等价__declspec(nothrow)
C4311
warning C4311: 'type cast' : pointer truncation from 'CQQ_EVENT_TYPE *__w64 ' to 'int'去掉/EP编译选项
C4390
warning C4390: ';' : empty controlled statement found; is this the intent? if (cmd->Load(pRootElement,"BeforeGet")); {
delete cmd;
return -1;
}
注意条件语句括号后的分号(;).不应该有此符号.
C4553
warning C4553: '==' : operator has no effect; did you intend '='?这实际上是新增配置引入的bug.
if (node)
do_cleanup_ == (bool)*node;
修改为:
do_cleanup_ = (bool)*node;
C4800
warning C4800: 'VARIANT_BOOL' : forcing value to bool 'true' or 'false' (performance warning)bool flg = (bool)ParamList[0].boolVal;
修改为:
bool flg = (bool)(ParamList[0].boolVal!=0);
C4805
warning C4805: '==' : unsafe mix of type 'short' and type 'bool' in operationvirtual short IsOnline() const = 0;
bool flag = server->IsOnline()==(bool)status;
修改为:
bool flag = server->IsOnline()==status;
C4995
warning C4995: 'safestr_strncpy': name was marked as #pragma deprecatedsafestr.h中有以下定义
#define strcpy safestr_strncpy
deprecated的函数是strcpy,而不是safestr_strncpy.(提示信息涉嫌误导)
#pragma warning(disable:4995)
C4996
warning C4996: 'sscanf': This function or variable may be unsafe. Consider using sscanf_s instead.warning C4996: 'stricmp': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _stricmp. See online help for details.
#pragma warning(disable:4996)