C++ STL-以下代码的问题是什么? #包括“stdafx.h” #包括 #包括 使用名称空间std; 类程序{ 私人: typedef映射昵称映射; 昵称地图昵称; 公众: void ShowEmailAddress(常量字符串和昵称)常量 { 昵称映射::常量迭代器i=昵称.find(昵称); if(i!=昵称.end()) { } } }; int main(int argc,char*argv[]) { printf(“你好,世界!\n”); 返回0; }

C++ STL-以下代码的问题是什么? #包括“stdafx.h” #包括 #包括 使用名称空间std; 类程序{ 私人: typedef映射昵称映射; 昵称地图昵称; 公众: void ShowEmailAddress(常量字符串和昵称)常量 { 昵称映射::常量迭代器i=昵称.find(昵称); if(i!=昵称.end()) { } } }; int main(int argc,char*argv[]) { printf(“你好,世界!\n”); 返回0; },c++,stl,C++,Stl,当我在VC6.0中编译上述代码时,我看到了大量的警告。如果我使用警告级别4并将所有警告视为错误,则STLFilt的输出错误如下所示: #include "stdafx.h" #include <string> #include <map> using namespace std; class NiftyEmailProgram { private: typedef map<string, string> NicknameMap; Nickna

当我在VC6.0中编译上述代码时,我看到了大量的警告。如果我使用警告级别4并将所有警告视为错误,则STLFilt的输出错误如下所示:

#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;
}
编译。。。
t3.cpp
c:\devstudio_6.0\vc98\include\xtree(118):错误C2220:警告视为错误-未生成任何对象文件
c:\devstudio_6.0\vc98\include\map(46):请参阅正在编译的类模板实例化“map”的参考
C:\TEMP\t3\t3.cpp(12):请参阅对正在编译的类模板实例化“map”的引用
执行cl.exe时出错。
t3.exe-1个错误,26个警告
工具返回的代码:0
现在,这段代码的问题是什么?我该如何修复它


谢谢

尝试发布未经处理的警告

然而,我也记得我在
中从
得到了一些可以安全忽略的4级警告(IIRC是无害的)

为了避免警告,我在STL中加入了一些适当的
#pragma警告
指令(附在正确的
#ifdef
指令中,以便仅在MSVC++上考虑这些指令,感谢@Alexandre C.提醒我):

\ifdef\u MSC\u VER
//禁用以下标题的C4702警告
#pragma警告(推送)
#杂注警告(禁用:4702)
#endif/\u MSC\u VER
//映射STL容器
#包括
//列表STL容器
#包括
//向量STL容器
#包括
#ifdef硕士学位
#布拉格警告(pop)
#恩迪夫
您也可以简单地将该部分中的警告级别降低到3(甚至更低),方法是:

#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
\ifdef\u MSC\u VER
//仅此部分将警告级别降低到3
#pragma警告(推,3)
#恩迪夫
//映射STL容器
#包括
//列表STL容器
#包括
//向量STL容器
#包括
#ifdef硕士学位
#布拉格警告(pop)
#endif/\u MSC\u VER

有关更多信息,请参阅。

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

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

正如其他人提到的,最好、最简单的修复方法是升级编译器

编辑:我在自己的VC6上试过,错误与我记忆中的完全一致:

#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

你为什么使用一个12年前的编译器?代码读起来很好。VC 6.0很可能是罪魁祸首。VC 6是罪魁祸首。VC 8反对使用一些完全有效的STL函数(例如,
std::copy
)。这每天都让我烦恼。您必须根据具体情况禁用警告。您应该询问他们是否计划升级到Windows 98。看看它是如何与VC6同步的。嘿,大家好,不要因为他保留了一个旧的编译器而责备他——有时你所依赖的库无法升级,或者大量的代码升级不划算。另外,我对我的答案做了一个补充,请查看。为了便于携带,您可以将
#pragma
#ifdef\u MSC\u VER
分开
#endif
@Alexandre C:嗯,你说得很对,我现在就修好。很抱歉吹毛求疵,但是当你这么做的时候,要养成为特定版本启用它的习惯,因为大家都知道它在MSVC7上表现得很好。@AndréCaron:我在MSVC7.1上也得到了4级警告,所以我还是保持原样,因为一般来说,您可能希望标准库标题经过足够的测试,因此您可能从中得到的警告并不重要。总的警告消息大小为60285,我不允许在此处发布这么大的文本块。感谢you@q0987:您可以将其发布在pastebin或类似的东西上。@q0987,我想大多数警告都是重复的,您不能只发布一两条吗?
#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
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
#pragma warning(disable:4786)