应该是逻辑异常了,实在是无能为力了,跪求大神

应该是逻辑错误了,实在是无能为力了,跪求大神!



//file name:List.h
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
template <typename T>
class List //定义线性表抽象类
{
public:
virtual void InitList()=0;
virtual void DestoryList()=0;
virtual int Length()=0;
virtual T Get(int i)=0;
virtual int Locate(T&x)=0;
virtual void Insert(int i,T & x)=0;
virtual T Delete(int i)=0;
virtual bool Empty()=0;
virtual bool Full()=0;
};
#endif



//file name:seqList.h
#ifndef SEQLIST_H
#define SEQLIST_H
#include <iostream>
#include "List.h"
using namespace std;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
void fuck()
{
cout<<"fuck you"<<endl;
}
template <typename T>
class SeqList:public List<T>
{
template <typename T>
friend ostream & operator<<(ostream & os,const SeqList<T> & s1);
 template<typename T>
friend SeqList<T> &operator+(SeqList<T> &,SeqList<T>&); 
template <typename T>
friend SeqList<T> Add(SeqList<T> A,SeqList <T> B);
public:
SeqList(int =20);
SeqList(T ary[],int n,int max);
~SeqList();
virtual void InitList(){curLen=0;}
virtual void DestoryList(){delete []ptr;}
virtual int Length(){return curLen;}
virtual T Get(int i);
virtual int Locate(T & x);
virtual void Insert (int i, T & x);
virtual T Delete(int i);
virtual bool Empty(){return curLen==0;}
virtual bool Full(){return curLen==max;}
void bubbleSort();
//int max(int a,int b);
//int Add(SeqList<T> A,SeqList<T> B);  //形参怎么写??
private:
int max;
T* ptr;
int curLen;//长度
};
template <typename T>
SeqList<T>::SeqList(int m)
{
max=m;
ptr=new T[max];
InitList();
}
template <typename T>
SeqList<T>::SeqList(T ary[],int n,int max)
{
this->max=max;
curLen=0;
ptr=new T[max];
while (curLen<n)
{
ptr[curLen]=ary[curLen];
curLen++;
}
}
template <typename T>
SeqList<T>::~SeqList()
{
DestoryList();
}
template <typename T>
T SeqList <T>::Get(int i)
{
if(i>=1&&i<=curLen)
return ptr[i-1];
else
throw "元素位置错误";
}
template <typename T>
int SeqList<T>::Locate(T & x)
{
for (int i=0;i<curLen;i++)
if(ptr[i]==x)
return i+1;
return 0;
}
template <typename T>
void SeqList<T>::Insert(int i,T&x)
{
if(Full())
throw"上溢";
if(i<1||i>curLen+1)
throw"插入位置错误";
for(int j=curLen;j>=i;j--)
ptr[j]=ptr[j-1];
ptr[i-1]=x;
curLen++;
}
template <typename T>
T SeqList <T>::Delete(int i)
{
T x;
if(Empty())
throw "空表";
if(i<1||i>curLen)
throw"删除位置错误";
x=ptr[i-1];
for(int j=i-1;j<curLen-1;j++)
ptr[j]=ptr[j+1];
curLen--;
return x;
}
template <typename T>
ostream & operator <<(ostream &os,const SeqList<T> &s1)//曰了狗了,为什么要重复?不重复还不行
{
for(int i=0;i<s1.curLen;i++)
os<<s1.ptr[i];
return os;
}
template <typename T>
SeqList<T> &operator+(SeqList<T> & s1, SeqList<T> & s2)
{
SeqList<int> temp=s1;
for (int i = 0; i < s2.curLen;i++)
{
if (temp.Locate(s2.ptr[i]) == 0) //元素不重复
temp.Insert(temp.curLen+1, s2.ptr[i]);
}
return temp;
}

template <typename T>
void SeqList<T>::bubbleSort()
{
T tmp;
for (int i=0;i<curLen-1;i++)
for(int j=0;j<curLen-i-1;j++)
if(ptr[j]>ptr[j+1])
{
tmp=ptr[j];
ptr[j]=ptr[j+1];
ptr[j+1]=tmp;
}
}
template <typename T>
 SeqList<T> Add(SeqList<T> A,SeqList<T> B) //注意书中代码与本程序代码中 长度,数组等变量以 本程序 为标准
{
T flag=0,i=0;
SeqList<T> C;
//int n=A.curLen;
//int m=B.curLen;   //求大整数A,B的位数
while(i<A.curLen && i<B.curLen)
{
C.ptr[i]=(A.ptr[i]+B.ptr[i]+flag)%10;  //计算第i位的值
flag=(A.ptr[i]+B.ptr[i]+flag)/10;      //计算第i位的进位
i++;
}
for(;i<A.curLen;i++)
{
C.ptr[i]=(A.ptr[i]+flag)%10;
flag=(A.ptr[i]+flag)/10;
}
for(;i<B.curLen;i++)
{
C.ptr[i]=(B.ptr[i]+flag)%10;
flag=(B.ptr[i]+flag)/10;
}
C.curLen=max(A.curLen,B.curLen)+flag;
if(flag==1)
C.ptr[C.curLen]=1;
return C;
//cout<<C.ptr[1];
}
#endif






//file name:002.cpp
#include <iostream>
#include"seqList.h"
//#include"student.h"
using namespace std;

void main()
{
int A[]={4,7,2,9,10,43,6},B[]={5,12,6,8,4,11,13,15,10},x=10;
SeqList<int> AList(A,6,10); // 模板类
SeqList<int> BList(B,9,20),CList; // 模板类
cout<<AList<<endl;
cout<<BList<<endl;

cout<<Add(AList,BList)<<endl;
cout<<"fuck you,你大爷"<<endl;
}
------解决思路----------------------
经验证,SeqList少个拷贝构造函数,导致add执行返回C的时候,临时对象构造不正确。


SeqList(const SeqList&);

------解决思路----------------------
引用:
经验证,SeqList少个拷贝构造函数,导致add执行返回C的时候,临时对象构造不正确。


SeqList(const SeqList&);

实现

template <typename T>
SeqList<T>::SeqList(const SeqList& src)
{
    this->curLen = src.curLen;
    this->max = src.max;
    this->ptr = new T[src.max];
    memcpy(this->ptr, src.ptr, sizeof(T) * src.curLen);
}