似乎是构造函数不对,但是看不出来哪里不对。
****head.h****
#include<iostream>
template<class type>
class seqlist {
public:
seqlist(int size){
maxsize = size;
length = 0;
start = new type[maxsize];
}
seqlist(std::istream& in) { read(in); }
~seqlist() { delete[]start; }
std::istream& read(std::istream&);
void simplify();
void cancel(int);
void output(std::ostream&);
private:
type* start;
int maxsize, length;
};
**head.cpp**
#include"head.h"
#include<iostream>
using namespace std;
template<class type> istream& seqlist<type>::read(istream& in) {
cout << "please input the length of the list:";
in >> maxsize;
start = new type[maxsize];
type x;
length = 0;
while (in >> x) {
*start++ = x;
length++;
}
return in;
}
template<class type> void seqlist<type>::cancel(int i) {
for (; i < length - 1; i++)
start[i] = start[i + 1];
}
template<class type> void seqlist<type>::simplify() {
for(int i=0;i<length;i++)
for (int j = i + 1; j < length; j++) {
if (start[i] == start[j])
cancel(j);
}
}
template<class type> void seqlist<type>::output(ostream& out) {
for (int i = 0; i < length; i++)
out << start[i] << " ";
}
**main.cpp**
#include"head.h"
#include<iostream>
using namespace std;
int main() {
seqlist<int> data(cin);
data.simplify();
data.output(cout);
}