程序源码:
#include<iostream>
using namespace std;
template <typename T1=int,typename T2=double>
class HoldsPair
{
private:
T1 Value1;
T2 Value2;
public:
HoldsPair(const T1& value1, const T2& value2)
{
Value1 = value1;
Value2 = value2;
};
const T1 & GetFirstvalue() const
{
return Value1;
};
const T2& GetSecondvalue() const
{
return Value2;
};
};
int main()
{
HoldsPair<> mIntFloatPair(300, 10.09);
HoldsPair<short,char*> mshortstringPair(25, "learn template,love c++");
cout << "the first object conntains-" << endl;
cout << "Value 1:" << mIntFloatPair.GetFirstvalue() << endl;
cout << "Value 2:" << mIntFloatPair.GetSecondvalue() << endl;
cout << "the second object contains-:" << endl;
cout << "Value 1:" << mshortstringPair.GetFirstvalue() << endl;
cout << "Value 2:" << mshortstringPair.GetSecondvalue() << endl;
return 0;
}
出现错误的原因是这一行:HoldsPair<short,char*> mshortstringPair(25, "learn template,love c++");
系统提示:没有与参数列表匹配的构造函数HoldsPair<T1,T2>::HoldsPair[其中T1=short,T2=char*]实例
把<short,char*>改成<short,const char*>就可以编译成功