程序异常退出??找不到错啊QAQ(类模板相关)
#include<iostream>using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int s)
{size=s;
int i;
for(i=0;i<size;i++)cin>>data[i];
}
MyArray(){;}
void sort();
void display();
bool check();
~MyArray();
private:
T data[101];
int size;
};
template<class T>
void MyArray<T>::sort()
{
int i,j;
T temp;
for(i=0;i<size-1;i++)
{
for(j=0;j<size-1-i;j++)
{
if(data[j]>data[j+1])
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}
}
}
}
template<class T>
void MyArray<T>::display()
{
int i;
for(i=0;i<size;i++)cout<<data[i]<<" ";
cout<<endl;
}
template<class T>
MyArray<T>::~MyArray(){ delete[] data;}
template<class T>
bool MyArray<T>::check(){
int i;
for(i=0;i<size-1;i++)
if(data[i]>data[i+1]) { cout<<"ERROR!"<<endl;return false;}
return true;
}
int main( )
{
MyArray<int> *pI;
MyArray<float> *pF;
MyArray<char> *pC;
int ty, size;
cin>>ty;
while(ty>0){
cin>>size;
switch(ty){
case 1: pI = new MyArray<int>(size); pI->sort(); pI->check(); pI->display(); delete pI; break;
case 2: pF = new MyArray<float>(size); pF->sort(); pF->check(); pF->display(); delete pF; break;
case 3: pC = new MyArray<char>(size); pC->sort(); pC->check(); pC->display(); delete pC; break;
}
cin>>ty;
}
return 0;
}