YYX_Olive 2022-09-24 18:19 采纳率: 100%
浏览 55
已结题

代码编译时报错:[错误] 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<Elem> &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<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 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

要怎么改?改哪里?

  • 写回答

3条回答 默认 最新

  • _GX_ 2022-09-24 23:53
    关注

    两处错误:

    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();
     
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月3日
  • 已采纳回答 9月25日
  • 创建了问题 9月24日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)