读书人

《C++程序设计原理与实践》中的那个子

发布时间: 2013-09-06 10:17:17 作者: rapoo

《C++程序设计原理与实践》中的那个头文件为什么会导致编译出错?

#include <unordered_map>
#include <utility>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
#include "../../_h_file_/std_lib_facilities.h"

int main()
{
typedef unordered_map< string, int > Com_rank;

Com_rank cr1;
cr1.insert( make_pair( "Google", 5 ) );
cr1.insert( make_pair( "Apple", 3 ) );
cr1.insert( make_pair( "Microsoft", 1 ) );

typedef unordered_map< string, int >::iterator Iter;
for( Iter p = cr1.begin(); p != cr1.end(); ++p )
cout << p->first << ", " << p->second << endl;

system( "pause" );
//keep_window_open();
return 0;
}
那个头文件可以在这里下载:
http://www.stroustrup.com/Programming/std_lib_facilities.h

现在这样是可以通过编译的,但是如果把那个头文件放到第一行就会编译出错。现在这个状态即使去掉那个头文件可以可以的。

错误提示:
错误1error C2977: “stdext::hash_map”: 模板 参数太多d:\software\program\microsoft visual studio 11.0\vc\include\unordered_map286121.1.try.461.01.unordered_map
错误2error C2133: “cr1”: 未知的大小d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp14121.1.try.461.01.unordered_map
错误3error C2512: “stdext::hash_map”: 没有合适的默认构造函数可用d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp14121.1.try.461.01.unordered_map
错误4error C2663: “std::_Hash<_Traits>::insert”: 4 个重载没有“this”指针的合法转换d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp15121.1.try.461.01.unordered_map
错误5error C2663: “std::_Hash<_Traits>::insert”: 4 个重载没有“this”指针的合法转换d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp16121.1.try.461.01.unordered_map
错误6error C2663: “std::_Hash<_Traits>::insert”: 4 个重载没有“this”指针的合法转换d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp17121.1.try.461.01.unordered_map
错误7error C2663: “std::_Hash<_Traits>::begin”: 4 个重载没有“this”指针的合法转换d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp20121.1.try.461.01.unordered_map
错误8error C2663: “std::_Hash<_Traits>::end”: 4 个重载没有“this”指针的合法转换d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp20121.1.try.461.01.unordered_map


错误9error C2227: “->first”的左边必须指向类/结构/联合/泛型类型d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp21121.1.try.461.01.unordered_map
错误10error C2227: “->second”的左边必须指向类/结构/联合/泛型类型d:\project\mvse2012\programming\21.1.try.461.01.unordered_map\21.1.try.461.01.unordered_map\unordered_map.cpp21121.1.try.461.01.unordered_map

[解决办法]
这个是头文件包含顺序问题。
这个头需要其他头文件的支持,不然他用到的东西没有定义。
你可以试着把这里的几个头文件包含指令
#include <unordered_map>
#include <utility>
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
添加到那个文件中去。

读书人网 >C++

热点推荐