[英]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.