STL - 以下代码有什么问题?

时间:2010-12-17 20:17:17

标签: c++ 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

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

谢谢

2 个答案:

答案 0 :(得分:3)

尝试发布未经处理的警告。

但是,我也记得我在<xtree><map>收到了一些4级警告,可以安全地忽略(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

有关详细信息,请参阅documentation of #pragma warning

答案 1 :(得分:2)

如果我没记错的话,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)