C4503警告?我该如何解决/摆脱它们?
-
10-10-2019 - |
题
这是我第一次尝试C ++ STL。我正在尝试使用地图构建多维关联阵列。例如:
typedef struct DA {
string read_mode;
string data_type;
void *pValue;
void *pVarMemLoc;
}DA;
int main()
{
map<string, map<string, map<string, map<string, map<string, DA*>>>>> DATA;
DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"] = new DA;
DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"] = new DA;
DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"] = new DA;
IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"]->read_mode = "file";
IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"]->read_mode = "poll";
IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"]->read_mode = "report";
return 0;
}
在VS2005中编译上述代码时,我收到了C4503警告的170。所有警告都涉及“超过装饰的名称长度,名称已被截断”。该程序似乎运行良好。
有人愿意花一些时间向我解释是什么原因引起了这些警告,我该如何解决?提前致谢 :)
Warning 1 warning C4503: 'std::map<_Kty,_Ty>::~map' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 2 warning C4503: 'std::map<_Kty,_Ty>::map' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 3 warning C4503: 'std::map<_Kty,_Ty>::operator []' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 4 warning C4503: 'std::_Tree<_Traits>::~_Tree' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 5 warning C4503: 'std::map<_Kty,_Ty>::operator []' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 6 warning C4503: 'std::_Tree<_Traits>::iterator::~iterator' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 7 warning C4503: 'std::_Tree<_Traits>::iterator::iterator' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
解决方案
如果您打算保持这个数据结构的怪物,除了禁用该警告外,您几乎无能为力:
#pragma warning(disable:4503)
其他提示
我不是禁用警告的粉丝,因为根据我的研究,这种警告可能会带来意想不到的后果,因此我更喜欢真正地分配问题。
这是我要重写代码的方式:
typedef struct DA {
string read_mode;
string data_type;
void *pValue;
void *pVarMemLoc;
}DA;
struct ROOM{
map<string, DA*> map;
};
struct DEPARTMENT{
map<string, ROOM> map;
};
struct FLOOR{
map<string, DEPARTMENT> map;
};
struct STAGE{
map<string, FLOOR> map;
};
struct LEVEL{
map<string, STAGE> map;
};
您可以这样使用:
int main()
{
LEVEL DATA;
DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom1"] = new DA;
DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom2"] = new DA;
DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom3"] = new DA;
...
etc
我的关注和最终解决方案主要来自 MSDN.
其他人建议您如何禁用警告。我建议您改用您的设计。使用比Map^5更抽象的抽象。或更改存储的数据结构。例如,使用地图而不是地图^5。
更新:
我的意思是您基本上有两个选择:
您可以根据需要使用的键,并具有尽可能多的字符串/级别:
struct Key3 { std::string x, y, z; };
typedef std::map<Key3, DA*> MyMap;
或者您构建某些通用的东西,每个级别可以保持DA*值和/或其他级别。
以这种方式声明(注意完成的报价)
map<string, map<string, map<string, map<string, map<string, DA*> > > > > DATA;
C ++识别 >>
作为换档操作员。
不隶属于 StackOverflow