STL - 以下代码有什么问题?
#include "stdafx.h"
#include <string>
#include <map>
using namespace std;
class NiftyEmailProgram {
private:
typedef map<string, string> NicknameMap;
NicknameMap nicknames;
public:
void ShowEmailAddress(const string& nickname) const
{
NicknameMap::const_iterator i = nicknames.find(nickname);
if ( i != nicknames.end() )
{
}
}
};
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
当我在VC6.0中编译上面的代码时,我看到了大量的警告。如果我使用警告级别 4 并将所有警告视为错误,则 STLFilt 的输出错误如下:
Compiling...
t3.cpp
c:\devstudio_6.0\vc98\include\xtree(118): error C2220: warning treated as error - no object file generated
c:\devstudio_6.0\vc98\include\map(46): see reference to class template instantiation 'map<string,string>' being compiled
C:\TEMP\t3\t3.cpp(12): see reference to class template instantiation 'map<string,string>' being compiled
Error executing cl.exe.
t3.exe - 1 error(s), 26 warning(s)
Tool returned code: 0
现在,这段代码有什么问题以及如何修复它?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试发布未处理的警告。
但是,我也记得我从
中的
收到了一些 4 级警告,可以安全地忽略它(IIRC 它是 C4702< /a>,这是无害的)。为了避免出现警告,我在 STL
#include
周围放置了一些适当的#pragma warning
指令(包含在正确的#ifdef
中)他们只考虑在 MSVC++ 上,感谢 @Alexandre C. 提醒我这一点):您也可以简单地将该部分中的警告级别降低到 3(甚至更低):
了解更多信息,请参阅
#pragma warning<的文档/代码>
。
Try to post the non-treated warnings.
However, I too remember that I got some level 4 warning from
<xtree>
in<map>
, that could be safely ignored (IIRC it was C4702, which is harmless).To avoid the warning, I put around the STL
#include
s some appropriate#pragma warning
directives (enclosed in the correct#ifdef
s to have them considered only on MSVC++, thanks to @Alexandre C. for reminding me of it):You could also simply lower the warning level to 3 (or even lower) just in that section with:
For more info, see the documentation of
#pragma warning
.如果我没记错的话,STL 创建的损坏函数名称很容易超出某些内置限制,这就是触发警告的原因。不幸的是,您使用 STLFilt 让我们看不到编译器生成的实际警告。
我发现的唯一解决方法是使用 typedef 和/或派生类来缩短模板中使用的名称。
正如其他人提到的,最好和最简单的解决方法是升级编译器。
编辑:我在自己的 VC6 上尝试了这个,错误与我记忆中的一模一样:
我之前提到的解决方法还不够,因为使用最短的类名可能迭代器的名称仍然超过 255人物。解决方案是将其放在
#include
之前:
If I remember correctly, the mangled function names created by STL easily exceed some built-in limit, and this is what triggers the warning. It's unfortunate that you used STLFilt to keep us from seeing the actual warnings produced by the compiler.
The only work-around I found was to use typedefs and/or derived classes to shorten the names used in the template.
As the others have mentioned, the best and easiest fix would be to upgrade your compiler.
Edit: I tried this on my own VC6 and the error was exactly as I remembered:
The workaround I mentioned earlier isn't enough, because with the shortest class name possible the name of an iterator still exceeds 255 characters. The solution is to put this before the
#include <map>
: