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

当我在 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

现在,这段代码有什么问题,我该如何解决?

谢谢

尝试张贴未经处理的警告。

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

为了避免警告,我在 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

您也可以在该部分中简单地将警告级别降低到 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

有关更多信息,请参阅#pragma warning文档

如果我没记错的话,STL 创建的被破坏的函数名称很容易超过一些内置限制,这就是触发警告的原因。 不幸的是,您使用 STLFilt 来阻止我们看到编译器产生的实际警告。

我发现的唯一解决方法是使用 typedef 和/或派生类来缩短模板中使用的名称。

正如其他人所提到的,最好和最简单的解决方法是升级您的编译器。

编辑:我在自己的 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

我之前提到的解决方法还不够,因为使用尽可能短的类名,迭代器的名称仍然超过 255 个字符。 解决方案是将其放在#include <map>

#pragma warning(disable:4786)

暂无
暂无

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

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