代码编译时报错:[错误] cannot declare 变量 's1' 到 be of abstract 类型 'Aset<int>'

问题遇到的现象和发生背景

题目

img

img

用代码块功能插入代码,请勿粘贴截图

编写的代码

#include
#include
#include
using namespace std;

template

class Set {
public:
//在集合中插入一个元素

    virtual void Insert(const Elem &e) = 0;

//在集合中删除一个值等于e的元素,如果删除成功则返回true,否则返回false

    virtual bool Remove(const Elem &e) = 0;

//获取最早加入到集合第一个元素,该元素的值记录在参数e中返回。如果集合为空,则返回false,否则发返回true。

    virtual bool GetFirstElement(Elem &e) = 0;

//获取最晚加入到集合的元素,该元素的值记录在参数e中返回。如果集合为空,则函数返回false,否则返回true。

    virtual bool GetLastElemnt(Elem &e) = 0;

//获取集合的元素的个数

    virtual int GetSize() = 0;

//获取集合所有元素的值记录在参数array中返回。函数返回集合元素的个数

    virtual int GetElements(Elem array[]) = 0;

//含进入集合的次序,依次打印输出集合中的元素

    virtual void Print() = 0;

//将集合s中的元素合并到当前集合中

    virtual void operator += (Set &s) = 0;

};

template

class Aset : public Set {

public:
    Elem *list;
    int maxn;
    int len;
    int pos;


    Aset() {
        maxn = 1000;
        len = 0;
        pos = 0;
        list = new Elem[maxn];
    }

    ~Aset() {
        delete[]list;
    };

    void Insert(const Elem &e) {
        len++;
        pos++;
        list[pos] = e;

    };

    bool Remove(const Elem &e) {
        for (int i = 1; i <= len; i++) {
            if (list[i] == e) {
                list[i] = 0;
                for (int j = i; j < len; j++) {
                    list[j] = list[j + 1];
                }
                len--;
                return true;
            }
        }
        return false;
    }

    bool GetFirstElement(Elem &e) {
        if (len == 0)
            return false;
        e = list[1];
        return true;
    }

    bool GetLastElement(Elem &e) {
        if (len == 0)
            return false;
        e = list[len];
        return true;
    }

    int GetSize() {
        return len;
    }

    int GetElements(Elem array[]) {
        for (int i = 1; i <= len; i++) {
            array[i] = list[i];
        }
        return len;
    }

    void Print() {
        for (int i = 1; i <= len; i++) {
            cout << list[i] << " ";
        }
        cout << endl;
    }

    void operator += (Set &s) {
        Elem *slist = new Elem;
        s.GetElements(slist);
        int size = len;
        int flag = 0;
        for (int i = 1; i <= s.GetSize(); i++) {
            flag = 0;
            for (int j = 1; j <= size; j++) {
                if (slist[i] == list[j]) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
                Insert(slist[i]);
        }
    }

};

int main() {
Aset s1;
Aset s2;
Aset s3;

int e;
cin >> e;
s1.Insert(e);
while (cin.get() != '\n') {
    cin >> e;
    s1.Insert(e);
}
cin >> e;
s2.Insert(e);
while (cin.get() != '\n') {
    cin >> e;
    s2.Insert(e);
}
cin >> e;
s3.Insert(e);
while (cin.get() != '\n') {
    cin >> e;
    s3.Insert(e);
}
//line 1
s1.GetFirstElement(e);
cout << e << "";
s1.GetLastElement(e);
cout << e << endl;
//line 2
cout << s2.GetSize() << endl;
//line 3
s3.Print();
//line 4
s1 += s2;
s1.Print();
//line 5
int a = 0;
int l = s3.GetSize();
for (int i = 1; i <= l; i++) {
    s3.GetLastElement(a);
    s3.Remove(a);
    s2.Remove(a);
}
s2.Print();

}

运行结果及报错内容

img

要怎么改?改哪里?

两处错误:

  1. 第22行拼写错误,把GetLastElemnt改为GetLastElement
  2. 第133行改为Aset<int> s1;
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
 
template<class Elem>
 
class Set {
    public:
//在集合中插入一个元素
 
        virtual void Insert(const Elem &e) = 0;
//在集合中删除一个值等于e的元素,如果删除成功则返回true,否则返回false
 
        virtual bool Remove(const Elem &e) = 0;
//获取最早加入到集合第一个元素,该元素的值记录在参数e中返回。如果集合为空,则返回false,否则发返回true。
 
        virtual bool GetFirstElement(Elem &e) = 0;
//获取最晚加入到集合的元素,该元素的值记录在参数e中返回。如果集合为空,则函数返回false,否则返回true。
 
        virtual bool GetLastElement(Elem &e) = 0;
//获取集合的元素的个数
 
        virtual int GetSize() = 0;
//获取集合所有元素的值记录在参数array中返回。函数返回集合元素的个数
 
        virtual int GetElements(Elem array[]) = 0;
//含进入集合的次序,依次打印输出集合中的元素
 
        virtual void Print() = 0;
//将集合s中的元素合并到当前集合中
 
        virtual void operator += (Set<Elem> &s) = 0;
 
};
 
template<class Elem>
 
class Aset : public Set<Elem> {
 
    public:
        Elem *list;
        int maxn;
        int len;
        int pos;
 
 
        Aset() {
            maxn = 1000;
            len = 0;
            pos = 0;
            list = new Elem[maxn];
        }
 
        ~Aset() {
            delete[]list;
        };
 
        void Insert(const Elem &e) {
            len++;
            pos++;
            list[pos] = e;
 
        };
 
        bool Remove(const Elem &e) {
            for (int i = 1; i <= len; i++) {
                if (list[i] == e) {
                    list[i] = 0;
                    for (int j = i; j < len; j++) {
                        list[j] = list[j + 1];
                    }
                    len--;
                    return true;
                }
            }
            return false;
        }
 
        bool GetFirstElement(Elem &e) {
            if (len == 0)
                return false;
            e = list[1];
            return true;
        }
 
        bool GetLastElement(Elem &e) {
            if (len == 0)
                return false;
            e = list[len];
            return true;
        }
 
        int GetSize() {
            return len;
        }
 
        int GetElements(Elem array[]) {
            for (int i = 1; i <= len; i++) {
                array[i] = list[i];
            }
            return len;
        }
 
        void Print() {
            for (int i = 1; i <= len; i++) {
                cout << list[i] << " ";
            }
            cout << endl;
        }
 
        void operator += (Set<Elem> &s) {
            Elem *slist = new Elem;
            s.GetElements(slist);
            int size = len;
            int flag = 0;
            for (int i = 1; i <= s.GetSize(); i++) {
                flag = 0;
                for (int j = 1; j <= size; j++) {
                    if (slist[i] == list[j]) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0)
                    Insert(slist[i]);
            }
        }
};
 
int main() {
    Aset<int> s1;
    Aset<int> s2;
    Aset<int> s3;
 
    int e;
    cin >> e;
    s1.Insert(e);
    while (cin.get() != '\n') {
        cin >> e;
        s1.Insert(e);
    }
    cin >> e;
    s2.Insert(e);
    while (cin.get() != '\n') {
        cin >> e;
        s2.Insert(e);
    }
    cin >> e;
    s3.Insert(e);
    while (cin.get() != '\n') {
        cin >> e;
        s3.Insert(e);
    }
    //line 1
    s1.GetFirstElement(e);
    cout << e << "";
    s1.GetLastElement(e);
    cout << e << endl;
    //line 2
    cout << s2.GetSize() << endl;
    //line 3
    s3.Print();
    //line 4
    s1 += s2;
    s1.Print();
    //line 5
    int a = 0;
    int l = s3.GetSize();
    for (int i = 1; i <= l; i++) {
        s3.GetLastElement(a);
        s3.Remove(a);
        s2.Remove(a);
    }
    s2.Print();
 
}

用代码块帖一下
Elem在哪里定义的呢?

我再重新贴一下代码块,遗漏的地方补回来了,看得舒服点


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

template<class Elem>

class Set {
    public:
//在集合中插入一个元素

        virtual void Insert(const Elem &e) = 0;
//在集合中删除一个值等于e的元素,如果删除成功则返回true,否则返回false

        virtual bool Remove(const Elem &e) = 0;
//获取最早加入到集合第一个元素,该元素的值记录在参数e中返回。如果集合为空,则返回false,否则发返回true。

        virtual bool GetFirstElement(Elem &e) = 0;
//获取最晚加入到集合的元素,该元素的值记录在参数e中返回。如果集合为空,则函数返回false,否则返回true。

        virtual bool GetLastElemnt(Elem &e) = 0;
//获取集合的元素的个数

        virtual int GetSize() = 0;
//获取集合所有元素的值记录在参数array中返回。函数返回集合元素的个数

        virtual int GetElements(Elem array[]) = 0;
//含进入集合的次序,依次打印输出集合中的元素

        virtual void Print() = 0;
//将集合s中的元素合并到当前集合中

        virtual void operator += (Set<Elem> &s) = 0;

};

template<class Elem>

class Aset : public Set<Elem> {

    public:
        Elem *list;
        int maxn;
        int len;
        int pos;


        Aset() {
            maxn = 1000;
            len = 0;
            pos = 0;
            list = new Elem[maxn];
        }

        ~Aset() {
            delete[]list;
        };

        void Insert(const Elem &e) {
            len++;
            pos++;
            list[pos] = e;

        };

        bool Remove(const Elem &e) {
            for (int i = 1; i <= len; i++) {
                if (list[i] == e) {
                    list[i] = 0;
                    for (int j = i; j < len; j++) {
                        list[j] = list[j + 1];
                    }
                    len--;
                    return true;
                }
            }
            return false;
        }

        bool GetFirstElement(Elem &e) {
            if (len == 0)
                return false;
            e = list[1];
            return true;
        }

        bool GetLastElement(Elem &e) {
            if (len == 0)
                return false;
            e = list[len];
            return true;
        }

        int GetSize() {
            return len;
        }

        int GetElements(Elem array[]) {
            for (int i = 1; i <= len; i++) {
                array[i] = list[i];
            }
            return len;
        }

        void Print() {
            for (int i = 1; i <= len; i++) {
                cout << list[i] << " ";
            }
            cout << endl;
        }

        void operator += (Set<Elem> &s) {
            Elem *slist = new Elem;
            s.GetElements(slist);
            int size = len;
            int flag = 0;
            for (int i = 1; i <= s.GetSize(); i++) {
                flag = 0;
                for (int j = 1; j <= size; j++) {
                    if (slist[i] == list[j]) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0)
                    Insert(slist[i]);
            }
        }
};

int main() {
    Aset() s1;
    Aset<int> s2;
    Aset<int> s3;

    int e;
    cin >> e;
    s1.Insert(e);
    while (cin.get() != '\n') {
        cin >> e;
        s1.Insert(e);
    }
    cin >> e;
    s2.Insert(e);
    while (cin.get() != '\n') {
        cin >> e;
        s2.Insert(e);
    }
    cin >> e;
    s3.Insert(e);
    while (cin.get() != '\n') {
        cin >> e;
        s3.Insert(e);
    }
    //line 1
    s1.GetFirstElement(e);
    cout << e << "";
    s1.GetLastElement(e);
    cout << e << endl;
    //line 2
    cout << s2.GetSize() << endl;
    //line 3
    s3.Print();
    //line 4
    s1 += s2;
    s1.Print();
    //line 5
    int a = 0;
    int l = s3.GetSize();
    for (int i = 1; i <= l; i++) {
        s3.GetLastElement(a);
        s3.Remove(a);
        s2.Remove(a);
    }
    s2.Print();

}