请电脑高手帮忙解达一下这个问题C++?
想问一下template哪里错了?#include<iostream>#include<cstdlib>usingnamespacestd;template<classb...
想问一下template哪里错了?
#include <iostream>
#include <cstdlib>
using namespace std;
template<class basetype>
class element
{ private:
basetype *p;
int maxlength;
int currentlength;
public:
element();
element(int max);
~element();
void add(basetype t);
bool full() const;
void erase();
friend ostream& operator<<(ostream& outs, const element<basetype>& a);
friend istream& operator>>(istream& ints, element<basetype>& a);
};
int main()
{ element<int> number;
cin>>number;
cout<<"the element in your class:"<<endl;
cout<<element;
element<char> character(10);
cout<<" enter the character element:"<<endl;
char m;
for(int i=0;i<10;i++)
{ cout<<"the NO."<<i+1<<" character is :";
cin>>m;
character.add(m);}
cout<<"the added character is :"<<endl;
cout<<character;
return 0;
}
template<class basetype>
element<basetype>::element():maxlength(0),currentlength(0)
{ p=NULL; }
template<class basetype>
element<basetype>::element(int max)
{ maxlength=max;
currentlength=0;
p=new basetype[max];
}
template<class basetype>
istream& operator>>(istream& ints, element<basetype>& a)
{ cout<<"how about the maxlength of your class"<<endl;
ints>>a.maxlength;
a.currentlength=0;
if(a.p!=NULL)
a.erase();
a.p=new basetype[a.maxlength];
cout<<"please enter "<<a.maxlength<<" elements in your class"<<endl;
for(int i=0;i<a.maxlength;i++)
{ cout<<"the NO."<<i+1<<" :";
ints>>a.p[i];
a.currentlength=a.currentlength+1;}
return ints;
}
template<class basetype>
ostream& operator<<(ostream& outs,const element<basetype>& a)
{ for(int i=0;i<a.current;i++)
cout<<a.p[i]<<" ";
return outs;}
template<class basetype>
bool element<basetype>::full() const
{ if (currentlength==maxlength)
return true;
else
return false;
}
template<class basetype>
void element<basetype>::add(basetype m)
{
if (full())
{ cout<<"failed"<<endl;
exit(1);
}
else
{ currentlength++;
p[currentlength]=m;
}
}
template<class basetype>
void element<basetype>::erase();
{ if (p!=NULL)
delete [] p;
}
template<class basetype>
element<basetype>::~template()
{ delete [] p;} 展开
#include <iostream>
#include <cstdlib>
using namespace std;
template<class basetype>
class element
{ private:
basetype *p;
int maxlength;
int currentlength;
public:
element();
element(int max);
~element();
void add(basetype t);
bool full() const;
void erase();
friend ostream& operator<<(ostream& outs, const element<basetype>& a);
friend istream& operator>>(istream& ints, element<basetype>& a);
};
int main()
{ element<int> number;
cin>>number;
cout<<"the element in your class:"<<endl;
cout<<element;
element<char> character(10);
cout<<" enter the character element:"<<endl;
char m;
for(int i=0;i<10;i++)
{ cout<<"the NO."<<i+1<<" character is :";
cin>>m;
character.add(m);}
cout<<"the added character is :"<<endl;
cout<<character;
return 0;
}
template<class basetype>
element<basetype>::element():maxlength(0),currentlength(0)
{ p=NULL; }
template<class basetype>
element<basetype>::element(int max)
{ maxlength=max;
currentlength=0;
p=new basetype[max];
}
template<class basetype>
istream& operator>>(istream& ints, element<basetype>& a)
{ cout<<"how about the maxlength of your class"<<endl;
ints>>a.maxlength;
a.currentlength=0;
if(a.p!=NULL)
a.erase();
a.p=new basetype[a.maxlength];
cout<<"please enter "<<a.maxlength<<" elements in your class"<<endl;
for(int i=0;i<a.maxlength;i++)
{ cout<<"the NO."<<i+1<<" :";
ints>>a.p[i];
a.currentlength=a.currentlength+1;}
return ints;
}
template<class basetype>
ostream& operator<<(ostream& outs,const element<basetype>& a)
{ for(int i=0;i<a.current;i++)
cout<<a.p[i]<<" ";
return outs;}
template<class basetype>
bool element<basetype>::full() const
{ if (currentlength==maxlength)
return true;
else
return false;
}
template<class basetype>
void element<basetype>::add(basetype m)
{
if (full())
{ cout<<"failed"<<endl;
exit(1);
}
else
{ currentlength++;
p[currentlength]=m;
}
}
template<class basetype>
void element<basetype>::erase();
{ if (p!=NULL)
delete [] p;
}
template<class basetype>
element<basetype>::~template()
{ delete [] p;} 展开
2个回答
展开全部
/*
how about the maxlength of your class:3
please enter 3 elements in your class
the NO.1 :12
the NO.2 :23
the NO.3 :34
the element in your class:
12 23 34
how about the maxlength of your class:5
please enter 5 elements in your class
the NO.1 :q
the NO.2 :w
the NO.3 :e
the NO.4 :r
the NO.5 :t
the added character is :
q w e r t
Press any key to continue
*/
#include <iostream>
#include <cstdlib>
using namespace std;
template<class basetype>
class element {
private:
basetype *p;
int maxlength;
int currentlength;
public:
element(int len = 10);
element(basetype a[],int max);
~element();
int getmaxlen() const { return maxlength; }
void setmaxlen(int len) { maxlength = len; }
int getcurrentlength() const { return currentlength; }
void setcurrentlength(int len) { currentlength = len; }
basetype *&getp() { return p; }
void add(basetype t);
bool full() const;
void erase();
friend ostream &operator<<(ostream &outs, const element<basetype> &a);
friend istream &operator>>(istream &ints, element<basetype> &a);
basetype &get(int i) const { return p[i]; }
void set(int i,const basetype &elem) { p[i] = elem; }
};
template<class basetype>
element<basetype>::element(int len):maxlength(len),currentlength(0) { p = NULL; }
template<class basetype>
element<basetype>::element(basetype a[], int max) {
currentlength = maxlength = max;
p = new basetype[max];
for(int i = 0; i < max; ++i)
p[i] = a[i];
}
template<class basetype>
istream &operator>>(istream &ints, element<basetype> &a) {
int i,max;
basetype elem;
cout << "how about the maxlength of your class:";
ints >> max;
if(a.getp() != NULL) a.erase();
a.getp() = new basetype[max]; // 注意getp()的用法
cout << "please enter "<<max<<" elements in your class\n";
for(i = 0;i < max;i++) {
cout << "the NO." << i + 1 << " :";
ints >> elem;
a.set(i,elem);
}
a.setmaxlen(max);
a.setcurrentlength(max);
return ints;
}
template<class basetype>
ostream &operator << (ostream &outs,const element<basetype> &a) {
for(int i = 0;i < a.getcurrentlength();i++)
outs << a.get(i) << " ";
outs << endl;
return outs;
}
template<class basetype>
bool element<basetype>::full() const {
return (currentlength == maxlength);
}
template<class basetype>
void element<basetype>::add(basetype m) {
if(full()) {
cout << "failed\n";
exit(1);
}
p[currentlength++] = m;
}
template<class basetype>
void element<basetype>::erase() {
if(p != NULL) delete [] p;
currentlength = 0;
maxlength = 0;
}
template<class basetype>
element<basetype>::~element<basetype>() {
delete [] p;
currentlength = 0;
maxlength = 0;
p = NULL;
}
int main() {
element<int> number;
cin >> number;
cout << "the element in your class:\n";
cout << number;
element<char> character;
cin >> character;
cout << "the added character is :" << endl;
cout << character;
return 0;
}
追问
是的,但是原来哪里错了?
追答
basetype *p; int maxlength; int currentlength;都是私有数据成员,在成员函数 istream &operator>>(istream &ints, element &a) 中,但凡对这三个成员的直接存取都是不允许的,例如
ints>>a.maxlength;
a.currentlength=0;
a.p=new basetype[a.maxlength];
其他函数中也有类似的问题。
2014-05-03
展开全部
main 中cout << element 错误。
erase 函数的实现,多加;
析构函数书写错误。
最关键的错误是重载<< >> 没成功
erase 函数的实现,多加;
析构函数书写错误。
最关键的错误是重载<< >> 没成功
追问
怎么改,麻烦指教
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询