请根据框架代码,补充实现模板函数。
示例输入:
1 3
int 1 2 3
除去第一行之外,有“1”行输入数据。每行数据第一个字符串为该行数据的类型标签,其后跟着“3”个该类型数据,数据之间以空白间隔。
示例输出:
1 2 3 Count of Get() = 6
将每行数据原样输出(任意两个数据之间用一个空格分开)。最后输出调用Get的次数。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
namespace
{
int CountGet = 0;
}
// put your code below for template functions Get and PutAndGet
// todo ↓ ____qcodep____
template <typename T>
void Get(T &value)
{
++CountGet;
cin >> value;
}
template <typename T>
void GetAndPut(T value)
{
Get(value);
cout << value << " ";
}
// todo ↑
int main()
{
int m, n;
string tag;
Get(m); //*1
Get(n); //*2
for (int i = 0; i < m; i++)
{
Get(tag); //*3
if (tag == "int")
{
GetAndPut<int>(n);
}
else if (tag == "double")
{
GetAndPut<double>(n);
}
else if (tag == "string")
{
GetAndPut<string>(n);
}
}
cout << "Count of Get() = " << CountGet << endl;
return 0;
}
现在这么写总会报错:(不知如何解决 622.cpp:53:4: error: no matching function for call to 'GetAndPut' GetAndPut(n); ^~~~~~~~~~~~~~~~~ 622.cpp:26:6: note: candidate function template not viable: no known conversion from 'int' to 'string' (aka 'basic_string') for 1st argument void GetAndPut(T value) ^ 1 error generated.