Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157298
  • 博文数量: 35
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-09 21:36
文章存档

2010年(10)

2009年(25)

我的朋友

分类: C/C++

2009-12-09 11:02:09

#if defined(_MSC_VER) && _MSC_VER <1400  
#pragma warning (disable:4786)  
#endif  

_MSC_VER 定义编译器的版本,VC++6.0就是1200
// VC 2005及其以上版本
#if defined(_MSC_VER) && _MSC_VER >= 1400

#pragma warning (disable:4786)
此warning产生的原因是因为标识符过长,超过了最大限定255个字。例如:
#define a_really_long_class_name a_really_really_really_really_really_really_really_ \
really_really_really_really_really_really_really_really_really_really_really_really_ \
really_really_really_really_really_really_really_really_really_really_really_really_really_really_really
class a_really_long_class_name
{
public:
a_really_long_class_name() {};
int m_data;
};
void main()
{
a_really_long_class_name test_obj;
test_obj.m_data = 12;
}
类名超过了255个字,使用时就会报4786的waring。在使用STL(C++标准模板库)的时候经常引发类似的错
误,尤其是vector,map这类模板类,模板中套模板,一不小心就超长了。例如:
template
class VeryLongClassNameA{};
template
class VeryLongClassNameB{};
template
class VeryLongClassNameC{};
template
class VeryLongClassNameD{};
class SomeRandomClass{};
typedef VeryLongClassNameD ClassD ;
typedef VeryLongClassNameC ClassC;
typedef VeryLongClassNameB ClassB;
typedef VeryLongClassNameA ClassA;
void SomeRandomFunction(ClassA aobj){}
void main()
{
ClassA AObj ;
SomeRandomFunction(AObj) ;
}
解决方法有两种,一种是直接定义别名:
#ifdef _DEBUG
#define VeryLongClassNameA A
#define VeryLongClassNameB B
#endif
另一种是屏蔽4786warning:
#pragma warning(disable : 4786)
注意屏蔽语句必须放在报错的模板类的引用声明(如#include )之前,否则还是不起作用。

阅读(1351) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~