为什么在codeblock上可以运行,vs上不可以,估计是指针或者数组的问题。。实在找不出来了求大神指点一二

时间:2023-01-21 08:39:08
#include <iostream>
using namespace std;


class outofbount{};
class illegalsize{};
template<class elemtype>
class seqlist           //:public list<elemtype>
{
private:
 elemtype*date;
 int currentlength;
 int maxsize;
 void doublespace();

public:
 seqlist(int initsize=10);
 ~seqlist(){delete[]date;}
 int length()const{return currentlength;}
 void add(const elemtype&x);
 void print(){for (int i=0;i<this->currentlength;i++) cout<<this->date[i]<<' ';}
 seqlist operator+( /*const seqlist &a,*/  const seqlist &b);
 seqlist &operator=(const seqlist &b);
};
template<class elemtype>
seqlist<elemtype>::seqlist(int initsize)
{if(initsize<=0) throw illegalsize();
 date=new elemtype[initsize];
 maxsize=initsize;
 currentlength=0;
}
template<class elemtype>
void seqlist<elemtype>::doublespace()
{elemtype*tmp=date;
maxsize*=2;
date=new elemtype[maxsize];
for(int i=0;i<currentlength;++i) date[i]=tmp[i];
delete[]tmp;}
template<class elemtype>
void seqlist<elemtype>::add(const elemtype &x){
if (currentlength==maxsize)
    doublespace();
    this->date[currentlength++]=x;
}
template<class elemtype>
seqlist<elemtype> seqlist<elemtype>::operator+(const seqlist<elemtype> &b)
        {int alen=this->currentlength;
         int blen=b.currentlength;
 seqlist<elemtype> c(alen+blen+2);
         for (int i=0;i<alen;i++){ c.date[i]=this->date[i];c.currentlength++;}
 for (int i=0;i<blen;i++){c.date[i+alen]=b.date[i];c.currentlength++;}
 return c;}
template<class elemtype>
seqlist<elemtype> &seqlist<elemtype>::operator=(const seqlist<elemtype> &b)
{   if(this==&b) return *this;
    this->currentlength=0;
int asiz=this->maxsize;int blen=b.currentlength;
    while(asiz<=blen)this->doublespace();
for(int i=0;i<blen;i++)this->date[i]=b->date[i];
this->currentlength=b.currentlength;
return *this;
}
int num(const char * type)
{if (type[0]=='i') return 0;if(type[0]=='d') return 1;if(type[0]=='c')return 2;return -1;}
int main(){
char type[2000];int n,m;
cin>>type;
cin>>n;
cin>>m;
switch (num(type)){
case 0:{
            seqlist<int>v(n),u(m);
        for(int i=0;i<n;i++)
           {int a;cin>>a;v.add(a);}
            for(int i=0;i<m;i++)
                {int a;cin>>a;u.add(a);}
        seqlist<int>q=v+u;q.print();break;}


case 1:{seqlist<double>v(n),u(m);
        for(int i=0;i<n;i++)
           {double a;cin>>a;v.add(a);}
            for(int i=0;i<m;i++)
                {double a;cin>>a;u.add(a);}
        seqlist<double>q=v+u;q.print();break;}


case 2:{seqlist<char>v(n),u(m);
        for(int i=0;i<n;i++)
           {char a;cin>>a;v.add(a);}
            for(int i=0;i<m;i++)
                {char a;cin>>a;u.add(a);}


        seqlist<char>q=v+u;q.print();break;}}

return 0;
}

6 个解决方案

#1


你还需要写一个复制构造函数

#2


我写了啊template<class elemtype>
 seqlist<elemtype>::seqlist(int initsize)
 {if(initsize<=0) throw illegalsize();
  date=new elemtype[initsize];
  maxsize=initsize;
  currentlength=0;

#3


引用 1 楼 iyomumx 的回复:
你还需要写一个复制构造函数

我写了啊template<class elemtype>
 seqlist<elemtype>::seqlist(int initsize)
 {if(initsize<=0) throw illegalsize();
  date=new elemtype[initsize];
  maxsize=initsize;
  currentlength=0;

#4


引用 2 楼 qq_24213837 的回复:
我写了啊template<class elemtype>
 seqlist<elemtype>::seqlist(int initsize)
 {if(initsize<=0) throw illegalsize();
  date=new elemtype[initsize];
  maxsize=initsize;
  currentlength=0;


引用 3 楼 qq_24213837 的回复:
Quote: 引用 1 楼 iyomumx 的回复:

你还需要写一个复制构造函数

我写了啊template<class elemtype>
 seqlist<elemtype>::seqlist(int initsize)
 {if(initsize<=0) throw illegalsize();
  date=new elemtype[initsize];
  maxsize=initsize;
  currentlength=0;


请搜索 拷贝构造函数

应该是这样的:

seqlist<elemtype>::seqlist(const seqlist<elemtype>&list){
...
}

#5


你写的那个不是复制构造函数。应该要这样:
template<class elemtype>
 seqlist<elemtype>::seqlist(const seqlist<elemtype>& other)
{
    //...
}

#6


gcc默认是返回值优化, 所以看不出来, 这应该需要move的构造, 
seqlist(seqlist && r)

#1


你还需要写一个复制构造函数

#2


我写了啊template<class elemtype>
 seqlist<elemtype>::seqlist(int initsize)
 {if(initsize<=0) throw illegalsize();
  date=new elemtype[initsize];
  maxsize=initsize;
  currentlength=0;

#3


引用 1 楼 iyomumx 的回复:
你还需要写一个复制构造函数

我写了啊template<class elemtype>
 seqlist<elemtype>::seqlist(int initsize)
 {if(initsize<=0) throw illegalsize();
  date=new elemtype[initsize];
  maxsize=initsize;
  currentlength=0;

#4


引用 2 楼 qq_24213837 的回复:
我写了啊template<class elemtype>
 seqlist<elemtype>::seqlist(int initsize)
 {if(initsize<=0) throw illegalsize();
  date=new elemtype[initsize];
  maxsize=initsize;
  currentlength=0;


引用 3 楼 qq_24213837 的回复:
Quote: 引用 1 楼 iyomumx 的回复:

你还需要写一个复制构造函数

我写了啊template<class elemtype>
 seqlist<elemtype>::seqlist(int initsize)
 {if(initsize<=0) throw illegalsize();
  date=new elemtype[initsize];
  maxsize=initsize;
  currentlength=0;


请搜索 拷贝构造函数

应该是这样的:

seqlist<elemtype>::seqlist(const seqlist<elemtype>&list){
...
}

#5


你写的那个不是复制构造函数。应该要这样:
template<class elemtype>
 seqlist<elemtype>::seqlist(const seqlist<elemtype>& other)
{
    //...
}

#6


gcc默认是返回值优化, 所以看不出来, 这应该需要move的构造, 
seqlist(seqlist && r)