即使我添加“iostream”标题,模板编译失败,不包括“字符串”标题!
//----------------------------------------------------------
// Header : Factory.h
//----------------------------------------------------------
template <typename TKey, typename TObject>
class Factory
{
public :
std::map<TKey, TObject*> m_mapReference;
void Register(TKey key,TObject *obj){
if(m_mapReference.find(key)==m_mapReference.end())
m_mapReference[key]=obj;
}
};
class A
{ };
class B : public A
{ };
//----------------------------------------------------------
// Main.cpp
//----------------------------------------------------------
#include <iostream>
//#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string strType = "B";
B b;
Factory< string, A > fact;
fact.Register(strType, &b);
}
vc \ include \ xfunctional(125):错误C2784:'bool std :: operator <(const std :: _ Hash <_Features&,const std :: _ Hash <_Features]&):无法推断出参数模板'const std :: _ Hash <_Features&'来自'const std :: string'
我想你已经发现了“意外包括”。执行需要std :: string的一些功能所以需要包含,因此std :: string的定义是“意外”可见的。
你不应该依赖这种意外包括,虽然没有办法确定你没有这样做。很可能是在另一个平台上,或标准库的不同实现,甚至是不再需要或包含的标准库的新版本。