【问题标题】:C4503 warnings? How do i solve/get rid of them?C4503 警告?我如何解决/摆脱它们?
【发布时间】:2011-04-29 00:19:18
【问题描述】:

这是我第一次尝试 C++ STL。我正在尝试使用 map 构建一个多维关联数组。例如:

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 中编译上述代码时,我收到了 170 个 C4503 警告。 所有警告都是关于“超出装饰名称长度,名称被截断”。 该程序似乎运行良好。

有人愿意花点时间向我解释导致这些警告的原因以及我该如何解决这些警告?在此先感谢:)

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

【问题讨论】:

  • 我可以建议您使用 boost::shared_ptr 而不是原始指针来存储您的 *DA 吗?否则,释放所有分配的内存很可能会成为一场噩梦。
  • 您好,ereOn,感谢您的建议。我需要更多地研究这个。我的程序实际上是一个由主exe调用的exe。在程序中,首先会分配一大堆(可能是数千个)*DA,然后随着程序运行,一些将被释放并根据来自控制命令的控制命令动态分配主程序。

标签: c++ string stl map name-decoration


【解决方案1】:

如果您打算保留这个数据结构的怪物,除了禁用它之外,您几乎无能为力:

#pragma warning(disable:4503)

【讨论】:

  • 嗨 PigBen,感谢您的回答。我不得不使用一些多维关联来存储我的数据,就像 PHP 数组可以做的那样。您还有其他选择可以建议我看看吗?
  • @justin:是的:在另一个问题中描述您的数据,并询问您应该如何用 C++ 表示它。
【解决方案2】:

以这种方式声明(注意完成的引号)

map<string, map<string, map<string, map<string, map<string, DA*> > > > > DATA;

C++ 将&gt;&gt; 识别为移位运算符。

【讨论】:

  • 这不是问题(MSVC 已经按照 OP 的预期方式对其进行了解析)
【解决方案3】:

其他人建议您如何禁用警告。我建议你重新考虑你的设计。使用比 map^5 更多的抽象。或者改变存储的数据结构。例如。使用地图而不是地图^5。

更新:

我的意思是你基本上有两种选择:

  • 您可以根据需要使用具有尽可能多的字符串/级别的键:

    struct Key3 { std::string x, y, z; }; typedef std::map&lt;Key3, DA*&gt; MyMap;

  • 或者您构建一些通用的东西,其中每个级别都可以保存 DA* 值和/或另一个级别。

【讨论】:

  • 您好,威尔克斯,感谢您的建议。我正在重新考虑如何最大限度地减少地图的数量。我能得到的最好的结果是map^2。具有上述数据结构map^5的缺点是什么?出于迭代目的,我至少需要 2 个级别的数据关联。您认为更好的方法是什么?
  • 注意:在结构体中你需要定义 operator
【解决方案4】:

我不喜欢禁用警告,因为根据我的研究,这个警告可能会导致意想不到的后果,所以我更愿意真正放弃这个问题。

以下是我将如何重写代码:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
相关资源
最近更新 更多