C++技巧(warningC4786错误解决方法)
在使用std::vector的过程中,考试提示的编译器报错如下:
C:\ program files \ VC98 \ include \ vector(61):警告C4786:'??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$ allocator @ D @ 2 @ @ STD @ @ @ 2 @ @ STD @ @ QAE @ PBV?$basic_string@DU?$char_t
raits@D@std@@V?$allocator@D@2@@1@0ABV?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@1@@Z:在浏览器信息中标识符被截断为“255”个字符
D:\ RTD \ rule loader \ treenode . h(15):请参见对正在编译的类模板实例化“std::vector,class std::allocator >,Class STD::allocator D::char _ traits,Class:: allocator > > >”的引用
此警告的原因是标识符太长,超过了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 _ CLASS _ REALLY _ REALLY _ CLASS _ NAME
A _ REALLY _ LONG _ CLASS _ NAME
{
public:
int m _ data;
};
void main()
{
a _ really _ long _ class _ name test _ obj;
test _ obj . m _ data = 12;
}
如果类名超过255个字,使用时会报错4786。在使用STL(C++标准模板库)时,经常会产生类似的错误,特别是对于vector、map等模板类。如果不小心在模板里设置了一个模板,那就太长了。示例:
template
classverylongclassname a { };
模板
类verlong class name b { };
template
class verlong class name c { };
模板
类very long class name { };
class SomeRandomClass { };
typedef verlong class named ClassD;
typedef verylongclassname c class c;
typedef verlong class name b class b;
typedef verlong class name a class a;
void SomeRandomFunction(class a AObj){ }
void main()
{
class a AObj;
SomeRandomFunction(AObj);
}
有两种解决方案。一种是直接定义别名:
# ifdef _ debug
# define veryrongclassname a
# define veryrongclassname b
# endif
另一种是阻塞4786 warning:
br/]# pragma warning(disable:4786)
注意屏蔽语句必须放在报告错误的模板类的引用声明(如#include)之前,否则仍然不会
0条评论