该程序未编译。有什么问题?模板类在C++中生成错误
#include<iostream>
#include<map>
using namespace std;
template<class T>class Data{
string header;
T data;
public:
Data(string h, T d){header = h, data = d;}
void WriteData()
{
cout<<header<<": "<<data<<endl;
}
};
int main(int argc, _TCHAR* argv[])
{
Data<int> idata("Roll", 100);
Data<string>sdata("Name","Jakir");
idata.WriteData();
sdata.WriteData();
return 0;
}
显示以下错误。
错误C2679:二进制 '< <':没有操作员发现它接受一个右边的操作数类型的 '的std :: string'(或没有可接受的转换) C:\程序文件(x86)的\微软的Visual Studio 10.0 \ VC \包括\ ostream的(679):可以是“标准:: basic_ostream < _Elem,_Traits> &的std ::操作者< <>(标准:: basic_ostream < _Elem,_Traits> &,常量字符* )' with [ _Elem = char, _Traits = std :: char_traits ]
while试图匹配参数列表'(std :: ostream,std :: string)' ..... \ maptest \ mapt \ mapt \ mapt.cpp(16):while compiling class template member function '无效数据::写数据(无效)' 与 [ T = INT ]
请帮助。这很紧急。
+1,有时候它是简单的事情:) – 2013-04-21 09:28:17
哦,我的上帝。我犯了什么错误。非常感谢。它毁了我的2个小时。再次感谢。 – 2013-04-21 09:45:35
@JakirHossain:我们都在那里;)祝你的项目好运 – 2013-04-21 09:46:31