简体   繁体   English

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;
}

When I compile the above code in VC6.0, I have seen tons of warnings.当我在 VC6.0 中编译上述代码时,我看到了大量警告。 If I use Warning Level 4 and treat all warnings as error, then the output error from STLFilt is as follows:如果我使用警告级别 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

Now, what is the problem of this code and how do I fix it?现在,这段代码有什么问题,我该如何解决?

Thank you谢谢

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).但是,我也记得我是从一定程度上4警告<xtree><map> ,可能被安全地忽略(IIRC它是C4702 ,这是无害的)。

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):为了避免警告,我在 STL #include周围放置了一些适当的#pragma warning指令(包含在正确的#ifdef以便仅在 MSVC++ 上考虑它们,感谢@Alexandre C.提醒我):

#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

You could also simply lower the warning level to 3 (or even lower) just in that section with:您也可以在该部分中简单地将警告级别降低到 3(甚至更低):

#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

For more info, see the documentation of #pragma warning .有关更多信息,请参阅#pragma warning文档

If I remember correctly, the mangled function names created by STL easily exceed some built-in limit, and this is what triggers the warning.如果我没记错的话,STL 创建的被破坏的函数名称很容易超过一些内置限制,这就是触发警告的原因。 It's unfortunate that you used STLFilt to keep us from seeing the actual warnings produced by the compiler.不幸的是,您使用 STLFilt 来阻止我们看到编译器产生的实际警告。

The only work-around I found was to use typedefs and/or derived classes to shorten the names used in the template.我发现的唯一解决方法是使用 typedef 和/或派生类来缩短模板中使用的名称。

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:编辑:我在自己的 VC6 上试过这个,错误和我记忆中的完全一样:

c:\program files\microsoft visual studio\vc98\include\xtree(118) : warning C4786: 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<cha
r,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::basi
c_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
        c:\program files\microsoft visual studio\vc98\include\map(46) : see reference to class template instantiation 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<cha
r> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocato
r<char> > >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' being compiled

The workaround I mentioned earlier isn't enough, because with the shortest class name possible the name of an iterator still exceeds 255 characters.我之前提到的解决方法还不够,因为使用尽可能短的类名,迭代器的名称仍然超过 255 个字符。 The solution is to put this before the #include <map> :解决方案是将其放在#include <map>

#pragma warning(disable:4786)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM