纠正错误,谢!代码没错误,但是没法执行,求解。
#include<cassert>template<class T>
class Array{
private:
T*list;
int size;
public:
Array(int sz=50);
Array(const Array<T>&a);
~Array();
Array<T>&operator=(const Array<T>&rhs);
T&operator[](int i);
const T &operator[](int i)const;
operator T*();
operator const T*()const;
int getSize()const;
void resize(int sz);
};
template<class T>
Array<T>::Array(int sz){
assert(sz>=0);
size=sz;
list=new T[size];
}
template<class T>
Array<T>::~Array(){
delete[]list;
}
template<class T>
Array<T>::Array(const Array<T>&a){
size=a.size;
list=new T[size];
for(int i=0;i<size;i++)
list[i]=a.list[i];
}
template<class T>
Array<T>&Array<T>::operator=(const Array<T>&rhs){
if(&rhs!=this){
if(size!=rhs.size){
delete[]list;
size=rhs.size;
list=new T(size);
}
for(int i=0;i<size;i++)
list[i]=rhs.list[i];
}
return*this;
}
template<class T>
T &Array<T>::operator[] (int n){
assert(n>=0&&n<size);
return list[n];
}
template<class T>
const T &Array<T>::operator[] (int n) const{
assert(n>=0&&n<size);
return list[n];
}
template<class T>
Array<T>::operator T*(){
return list;
}
template<class T>
Array<T>::operator const T*() const{
return list;
}
template<class T>
int Array<T>::getSize() const{
return size;
}
template<class T>
void Array<T>::resize (int sz){
assert(sz>=0);
if(sz==size)
return;
T*newlist=new T[sz];
int n=(sz<size)?sz:size;
for(int i=0;i<n;i++)
newlist[i]=list[i];
delete[]list;
list=newlist;
size=sz;
}
#include"Array.h"
#include<iostream>
#include<string>
using namespace std;
class Contacts
{
private:
string name;
string adress;
string number;
string post;
string QQ;
public:
Contacts(string name,string adress,string number,string post,string QQ);
~Contacts(){}
/*void add_contact(std::string newname,std::string newadress,std::string newnumber,std::string newpost,std::string newQQ);*/
void show_contact();
void edit_contacts(string name,string adress,string number,string post,string QQ);
};
Contacts::Contacts(string name,string adress,string number,string post,string QQ): name(name),adress(adress), number(number), post(post),QQ(QQ)
{
cout<<name<<"is created"<<endl;
}
/*void Contacts::add_contact(std::string newname,std::string newadress,std::string newnumber,std::string newpost,std::string newQQ)
{
cout<<"name:";
cin>>name;
cout<<"adress";
cin>>adress;
cout<<endl<<"number:";
cin>>number;
cout<<endl<<"post";
cin>>post;
cout<<endl<<"QQ";
cin>>QQ;
cout<<endl<<"contact is created!"<<endl;
}*/
void Contacts::show_contact()
{
cout<<"name"<<name<<endl;
cout<<"number"<<number<<endl;
cout<<"adress"<<adress<<endl;
cout<<"post"<<post<<endl;
cout<<"QQ"<<QQ<<endl;
}
void Contacts::edit_contacts(string name,string adress,string number,string post,string QQ)
{
name=name;
adress=adress;
number=number;
post=post;
QQ=QQ;
}
int main()
{
string name;
string adress;
string number;
string post;
string QQ;
/*Contacts c1("zhangsan","yantai","123","123@.com","35689");
Contacts c2("lisi","zaozhuang","456","456@.com","78912");
Contacts*Contacts[]={&c1,&c2};*/
Array<class Contacts*>Contacts(0);
cout<<"(a)add_contact,(d)delete_contact,(s)show_contact,(c)correct_contact,(e)exit"<<endl;
/* const int n=sizeof(Contacts);*/
char cmd;
do{
int n;
Array<class Contacts*>Contacts;
cin>>cmd;
switch(cmd){
case'a':
cout<<"please enter name:";
cin>>name;
cout<<"please enter number:";
cin>>number;
cout<<"please enter adress:";
cin>>adress;
cout<<"please enter post";
cin>>post;
cout<<"please enter QQ";
cin>>QQ;
Contacts.resize(Contacts.getSize()+1);
Contacts[Contacts.getSize()-1]->edit_contacts(name, adress, number, post, QQ);
break;
case'd':
cout<<"the one you want to delete"<<endl;
cin>>n;
delete Contacts[n-1];
for(n;n<Contacts.getSize();)
Contacts[n]=Contacts[++n];
Contacts.resize(Contacts.getSize()+1);
break;
case's':
for( n=0;n<Contacts.getSize();n++)
{
cout<<"["<<n<<"]"<<endl;
Contacts[n]->show_contact();
cout<<endl;
}
break;
case'c':
cin>>n;
cout<<"please enter name:";
cin>>name;
cout<<"please enter number:";
cin>>number;
cout<<"please enter adress:";
cin>>adress;
cout<<"please enter post";
cin>>post;
cout<<"please enter QQ";
cin>>QQ;
Contacts[n]->edit_contacts(name, adress, number,post, QQ);
break;
}
}while(cmd!='e');
return 0;
}