求助,debug了一天,未果。救救新手。。。
template <typename T>
class dataList
{
private:
int arraySize;
void swap(const int m1, const int m2);//swap函数声明
int minKey(const int low, const int high);//minKey函数声明
public:
T *element;
dataList(int size = 10): arraySize(size),element(new T[size]){}
~dataList(){delete [] element;}
void sort();//sort函数
};
//swap函数定义
template <typename T>
void dataList<T> :: swap(const int m1, const int m2)
{
T temp = element[m1];
element[m1]=element[m2];
element[m2]= temp;
}
//minkey函数定义
template <typename T>
int dataList<T>::minKey(const int low, const int high)
{
int min = low;
for (int k=low+1;k<=high;k++)
if(element[k] < element[min]) min = k;
return min;
}
//sort函数定义
template <typename T>
void dataList<T>::sort()
{
for(int i =0; i<arraySize-1; i++)
{
int j = minKey(i, arraySize-1);
if (j!= i ) swap(j,i);
}
}
#include <iostream>
#include <fstream>
#include "sorting1.h"
using namespace std;
void main()
{
ifstream inData;
ofstream outData;
inData.open("DataIn.txt");
outData.open("DataOut.txt");
cout<<"Please input several numbers to the file .The program will write them in an acsend sequence.";
cout<<"Please input the quantity of the numbers."<<endl;
int quantity;
cin>>quantity;
dataList<int> list1(quantity);
int i=0;
while(inData)
{
inData>>list1.element[i];
i++;
}
cout<<"The numbers you input are:"<<endl;
for(int j=0;j<i-1;j++)
{
cout<<list1.element[j]+" ";
}
list1.sort();
for(int m=0;m<i-1;m++)
{
outData<<list1.element[m]+" ";
}
cout<<"The sorted list is:"<<endl;
for(int n=0;n<i-1;n++)
{
cout<<list1.element[n]+" ";
}
}
学会调试了,改了好多地方,好不容易运行到最后出现错误,无法生成结果。请各位帮看一下,万分感激。~
[解决办法]
hao le
template <typename T > class dataList { private: int arraySize; void swap(const int m1, const int m2);//swap函数声明 int minKey(const int low, const int high);//minKey函数声明 public: T *element; dataList(int size = 10): arraySize(size),element(new T[size]){} ~dataList(){delete [] element;} void sort();//sort函数 }; //swap函数定义 template <typename T > void dataList <T > :: swap(const int m1, const int m2) { T temp = element[m1]; element[m1]=element[m2]; element[m2]= temp; } //minkey函数定义 template <typename T > int dataList <T >::minKey(const int low, const int high) { int min = low; for (int k=low+1;k <=high;k++) if(element[k] < element[min]) min = k; return min; } //sort函数定义 template <typename T > void dataList <T >::sort() { for(int i =0; i <arraySize-1; i++) { int j = minKey(i, arraySize-1); if (j!= i ) swap(j,i); } } #include <iostream > #include <fstream > #include "sorting1.h" using namespace std; void main() { ifstream inData; ofstream outData; inData.open("DataIn.txt"); if (!inData.is_open()) { // Dump the contents of the file to cout. cout <<"indata error"<<endl; inData.close(); } outData.open("DataOut.txt"); if (!outData.is_open()) { // Dump the contents of the file to cout. cout <<"outdata error"<<endl; outData.close(); } cout <<"Please input several numbers to the file .The program will write them in an acsend sequence."<<endl; cout <<"Please input the quantity of the numbers." <<endl; int quantity; cin >>quantity; dataList <int> list1(quantity); int i=0; while(inData) { inData >>list1.element[i]; i++; } cout <<"The numbers you input are:" <<endl; for(int j=0;j <i-1;j++) { cout <<list1.element[j]<<" "; } cout<<endl; list1.sort(); for(int m=0;m <i-1;m++) { outData <<list1.element[m]<<" "; // 有问题的地方 } outData<<endl; cout <<"The sorted list is:" <<endl; for(int n=0;n <i-1;n++) { cout <<list1.element[n]<<" "; // 有问题的地方 } cout<<endl; outData.close(); inData.close(); system("pause");}