处理vs2005警告错误

对das 2.0项目的所有工程全面检查编译警告错误,处理记录汇集成本文.

此前的项目没有有意识地对警告进行处理,因此编译时经常报出大量的警告.
除了不清爽的感觉外,无视警告可能遗漏了对改进程序有价值的线索。

本文列举了所遇到的几乎全部的警告.

对每种错误提供以下信息:示例错误信息(黑色斜体表示),处理方式(红色)以及说明.

对这些警告的处理方式可以应用到其它项目,也包括das从中继承过来的.

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 called
            
class 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 data

typedef __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 does

static 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 operation
    virtual 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 deprecated


safestr.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)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值