STL-以下代码是什么问题? [英] STL - what is the problem of the following code?
问题描述
#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的输出错误如下:
When I compile the above code in VC6.0, I have seen tons of warnings. If I use Warning Level 4 and treat all warnings as error, then the output error from STLFilt is as follows:
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
现在,这段代码有什么问题,我该如何解决?
Now, what is the problem of this code and how do I fix it?
谢谢
推荐答案
尝试发布未经处理的警告.
Try to post the non-treated warnings.
但是,我也记得我从< map>
中的< xtree>
收到了4级警告,可以安全地忽略它(IIRC是 C4702 ,这是无害的.
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).
为避免警告,我在STL #include
中放置了一些适当的 #pragma warning
指令(包含在正确的 #ifdef
中)可以让它们仅在MSVC ++上考虑,这要感谢 @Alexandre C.提醒我):
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):
#ifdef _MSC_VER
//Disable the C4702 warning for the following headers
#pragma warning(push)
#pragma warning(disable:4702)
#endif // _MSC_VER
//map STL container
#include <map>
//list STL container
#include <list>
//vector STL container
#include <vector>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
您也可以仅在该部分中将警告级别降低至3(甚至更低):
You could also simply lower the warning level to 3 (or even lower) just in that section with:
#ifdef _MSC_VER
// Lower the warning level to 3 just for this section
#pragma warning(push, 3)
#endif
//map STL container
#include <map>
//list STL container
#include <list>
//vector STL container
#include <vector>
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
有关更多信息,请参见文档 #pragma警告
.
For more info, see the documentation of #pragma warning
.
这篇关于STL-以下代码是什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!