【发布时间】:2017-03-08 03:33:53
【问题描述】:
所以我有这个任务要求我使用函数模板从键盘加载数组数据,打印每个数组中的两个最小值,按降序对数组进行排序,将数据保存到文本文件,然后检索文本文件。 我的主要问题是我的 smallPrint 函数模板不断出现错误。一条错误消息显示: “没有函数模板“smallPrint”的实例与参数列表匹配 参数类型是:(float [7], int, int, const int) 当我尝试运行程序时弹出的另一件事是我的 printArray 函数模板中的一个问题。我没看出有什么问题。
非常感谢任何帮助。
这是作业:
"修改 arrtemp.cpp,删除每个数组的数据重新分配并添加五个新函数模板。 1.) 第一个新的函数模板应该允许用户从键盘输入数组数据。 2.) 第二个新函数模板应打印数组的最小值和第二最小值,而不进行排序。您可以假设每个数组都包含不同的数据。 3.) 第三个新函数模板应按降序对数组进行排序。 4.) 第四个新功能模板应将保存数据保存到文本文件中。 5.) 第五个新函数模板应该从文本文件中检索数组数据。输出应包括每个数组的最小值和次最小值,同时所有三个数组都按降序打印两次,一次在保存文本文件之前,一次在检索文本文件之后。确保在每个功能模板之前包含 [template ]。每个数组都应保存到单独的文本文件中。您还需要显示所有 3 个文本文件。”
下面是arrtemp.cpp和我写的:
arrtemp.cpp
#include <iostream>
using namespace std;
//function template to print an array
//works for multiple data types
template <class T>
void printarray (T *a, const int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int main()
{
const int n1 = 5, n2 = 7, n3 = 6;
int a[n1] = {2, 4, 6, 8, 10};
float b[n2] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
char c[n3] = "HELLO";
cout <<"the integer array" << endl;
printarray(a, n1);
cout <<"the float array" << endl;
printarray(b,n2);
cout <<"the string is" << endl;
printarray(c,n3);
return 0;
}
我写的:
#include<iostream>
#include<fstream>
using namespace std;
template <class T>
T loadArray(T *a, const int n )
{
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
}
template <class T>
T smallPrint(T *a, T *small, T *small2, const int n)
{
small = a[0];
small2 = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] < small)
small = a[i];
}
for (int i = 1; i < n; i++)
{
if (a[i] > small && a[i] < small2)
small2 = a[i];
}
cout << small << ' ' << small2;
cout << endl << endl;
}
template <class T>
T sort(T *a, const int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (a[j] < a[j + 1])
{
T t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
template <class T>
T saveFile(T *a, T small, T small2, const int n, string filename)
{
ofstream outfile(filename, ios::out);
for (int i = 0; i < n; i++)
{
outfile << a[i] << ' ';
}
outfile << small << small2;
outfile.close();
}
template <class T>
T retrieveFile(T *a, T small, T small2, const int n, string filename)
{
ifstream infile(filename, ios::in);
for (int i = 0; i < n; i++)
{
infile >> a[i];
}
infile >> small >> small2;
infile.close();
}
template <class T>
T printArray(T*a, const int n)
{
for (int i = 0; int < n; i++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
const int n1 = 5, n2 = 7, n3 = 6;
int a[n1];
float b[n2];
char c[n3];
int smalli, smalli2;
float smallf, smallf2;
char smallc, smallc2;
loadArray(a, n1);
loadArray(b, n2);
loadArray(c, n3);
smallPrint(a, smalli, smalli2, n1);
smallPrint(b, smallf, smallf2, n2);
smallPrint(c, smallc, smallc2, n3);
sort(a, n1);
sort(b, n2);
sort(c, n3);
printArray(a, n1);
printArray(b, n2);
printArray(c, n3);
saveFile(a, smalli, smalli2, n1, "i:\\ints.txt");
saveFile(b, smallf, smallf2, n2, "i:\\floats.txt");
saveFile(c, smallc, smallc2, n3, "i:\\chars.txt");
retrieveFile(a, smalli, smalli2, n1, "i:\\ints.txt");
retrieveFile(b, smallf, smallf2, n2, "i:\\floats.txt");
retrieveFile(c, smallc, smallc2, n3, "i:\\chars.txt");
printArray(a, n1);
printArray(b, n2);
printArray(c, n3);
system("PAUSE");
return 0;
}
这是有效的更正版本:
#include<iostream>
#include<fstream>
using namespace std;
template <class T>
void loadArray(T *a, const int n)
{
for (int i = 0; i < n; i++)
{
cout << "Enter a number (" << n-i << " left): ";
cin >> a[i];
}
cout << endl;
}
template <class T>
void smallPrint(T *a, T &small, T &small2, const int n)
{
small = a[0];
small2 = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] < small)
small = a[i];
}
for (int i = 1; i < n; i++)
{
if (a[i] > small && a[i] < small2)
small2 = a[i];
}
cout << small << ' ' << small2;
cout << endl << endl;
}
template <class T>
void sort(T *a, const int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (a[j] < a[j + 1])
{
T t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
template <class T>
void saveFile(T *a, T small, T small2, const int n, string filename)
{
ofstream outfile(filename, ios::out);
for (int i = 0; i < n; i++)
{
outfile << a[i] << ' ';
}
outfile << small << ' ' << small2;
outfile.close();
}
template <class T>
void retrieveFile(T *a, T small, T small2, const int n, string filename)
{
ifstream infile(filename, ios::in);
for (int i = 0; i < n; i++)
{
infile >> a[i];
}
infile >> small >> small2;
infile.close();
}
template <class T>
void printArray(T*a, const int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
const int n1 = 5, n2 = 7, n3 = 6;
int a[n1];
float b[n2];
char c[n3];
int smalli, smalli2;
float smallf, smallf2;
char smallc, smallc2;
loadArray(a, n1);
loadArray(b, n2);
loadArray(c, n3);
smallPrint(a, smalli, smalli2, n1);
smallPrint(b, smallf, smallf2, n2);
smallPrint(c, smallc, smallc2, n3);
sort(a, n1);
sort(b, n2);
sort(c, n3);
printArray(a, n1);
printArray(b, n2);
printArray(c, n3);
saveFile(a, smalli, smalli2, n1, "i:\\ints.txt");
saveFile(b, smallf, smallf2, n2, "i:\\floats.txt");
saveFile(c, smallc, smallc2, n3, "i:\\chars.txt");
retrieveFile(a, smalli, smalli2, n1, "i:\\ints.txt");
retrieveFile(b, smallf, smallf2, n2, "i:\\floats.txt");
retrieveFile(c, smallc, smallc2, n3, "i:\\chars.txt");
printArray(a, n1);
printArray(b, n2);
printArray(c, n3);
system("PAUSE");
return 0;
}`
【问题讨论】:
标签: c++ arrays visual-studio templates function-templates