2 apprentices apprentices 于 2016.03.13 12:50 提问

数据结构关于输出的问题,求老司机指路

#include

#include

using namespace std;

template

class SeqList

{

protected:

T *element;

int length;

int n;

private:

void init(T value[],int n);

public:

SeqList(int length = 32);

SeqList(int length, T n);

SeqList(T value[], int n);

~SeqList();


bool empty();

int  count();

T & operator [](int i);

friend ostream& operator <<<>(ostream&, SeqList<T>&);

void priintPrevious();

void insert(int i,T x);

virtual void insert(T x);

T remove(int i);

void removeAll();

virtual int search(T key, int start = 0);

virtual void removeFirst(T key);


SeqList(SeqList<T>&list);

SeqList<T>&operator =(SeqList<T>&list);

bool operator==(SeqList<T>&list);

bool operator!=(SeqList<T>&list);

void operator+=(SeqList<T>&list);

SeqList<T>operator+(SeqList<T>&list);

};
template
SeqList ::SeqList(int length) //构造空顺序表,length指定数组容量

{
if (length < 0)

    length = 0;

this->element = new T[length];

this->length = length;

this->n = 0;                                //元素个数为0

}

template

SeqList ::SeqList(int length, T n) //构造顺序表,length个元素为X,构造函数重载
{

this->element = new T[length];

this->length = this->n = length;

for (int i; i < this->n; i++)

    this->element[i] = x;                   //执行T的幅值

}

template

SeqList ::SeqList(T value[], int n) //构造顺序表,由value数组提供元素,n指定元素个数

{
this->init(value, n);
}

template

void SeqList ::init(T value[], int n) //初始化顺序表

{
this->length = n * 2;

this->element = new T[this->length];

this->n = n;

for (int i = 0; i < n; i++)

    this->element[i] = value[i];

}
template

SeqList ::~SeqList() //析构函数

{
delete[]this->element; //释放掉element所占用的内存

}
//template

//template
int main()
{
int value[3] = {0,1,2};
SeqList seqlist(int value,int n=3);
cout<<<element[2]; //这里的输出语句如何写

return 0;

}

2个回答

qq772615962
qq772615962   2016.03.13 23:16

你的变量为protect,外部访问不了,可以专门写个public函数来返回类的element的值

apprentices
apprentices   2016.03.14 20:22

不可以,没有函数可以返回一个数组,只能返回数组首地址

Csdn user default icon
上传中...
上传图片
插入图片