iostream与string问题
//xxx.h
#include <iostream>
#include <map>
using namespace std;
map<string,string> m_map_dbsettings;
//xxx.cpp
string strItem = "test";
string strValue = "2";
m_map_dbsettings[strItem] = strValue;//这个赋值引发的编译错误
编译提示下面错误,如果在cpp里面加上#include <string> 就可以编译正常了。
有人知道原因吗,iostream里面不是有string吗
e:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: “bool std::operator <(const std::deque<_Ty,_Alloc> &,const std::deque<_Ty,_Alloc> &)”: 未能从“const std::string”为“const std::deque<_Ty,_Alloc> &”推导 模板 参数
1> e:\program files\microsoft visual studio 10.0\vc\include\deque(1725) : 参见“std::operator <”的声明
1> e:\program files\microsoft visual studio 10.0\vc\include\xfunctional(124): 编译类 模板 成员函数“bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const”时
1> with
1> [
1> _Ty=std::string
1> ]
1> e:\program files\microsoft visual studio 10.0\vc\include\map(71): 参见对正在编译的类 模板 实例化“std::less<_Ty>”的引用
1> with
1> [
1> _Ty=std::string
1> ]
1> e:\program files\microsoft visual studio 10.0\vc\include\xtree(451): 参见对正在编译的类 模板 实例化“std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>”的引用
1> with
1> [
1> _Kty=std::string,
1> _Ty=std::string,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std::pair<const std::string,std::string>>,
1> _Mfl=false
1> ]
1> e:\program files\microsoft visual studio 10.0\vc\include\xtree(520): 参见对正在编译的类 模板 实例化“std::_Tree_nod<_Traits>”的引用
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>
1> ]
1> e:\program files\microsoft visual studio 10.0\vc\include\xtree(659): 参见对正在编译的类 模板 实例化“std::_Tree_val<_Traits>”的引用
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>
1> ]
1> e:\program files\microsoft visual studio 10.0\vc\include\map(81): 参见对正在编译的类 模板 实例化“std::_Tree<_Traits>”的引用
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>
1> ]
[解决办法]
即便是有,你也要include,因为你显式地使用了std::string,这是基本的编程规范。
------解决方案--------------------
这个说不好,可能与编译器也有关系,有的string不用显示添加也能正常使用,不过为了养成好习惯,还是现实添加的好
[解决办法]
这个要看不同的编译器。不过VS好像是包含了string的。
但是你的代码中用了std::string就需要包含<string>。
为了规范用到了string还是包含<string>头文件好点。
[解决办法]
谁跟你扯的iostream里带了string的头文件的,离那家伙远点!
[解决办法]
多加一个
#include <string>
不会累死人的!
[解决办法]
using namespace std;
只是把 std 名空间的所有名字导入, 全局名空间或者 某个局部作用域;
这个名字有没有定义,还要看你的程序有没有相关的代码,比如有没有#include<string>
如果你#include "xx.h" 指令前有#include<string>
那么#include<string>
可写可不写
否则不写必错
当然,如果编译器认为不写,就等于写了的除外,相信一般编译器,都不是这样的。
类似的例子是
TC2.0 中
printf
scanf
不必
#include<stdio.h>
就可以使用了。
相信,这种例外不多。