c++ - 无法将字段声明为嵌套模板中的抽象类型

标签 c++ class templates abstract-class

我正在尝试制作一个抽象的播放列表,使用模板 setInterface.h 的限定符,并制作 Set.h、Playlist.h 和 Song.h。

在我添加 Playlist.h 之前一切正常,当我收到“错误:无法将字段‘PlayList::playlist_’声明为抽象类型‘Set’”时。

代码如下:

设置界面.h

    #ifndef SET_INTERFACE_H_

    #define SET_INTERFACE_H_

    #include <vector>

    template<class ItemType>

    class SetInterface{

    public:

       /** Gets the current number of entries in this set.
    */
       virtual int getCurrentSize() const = 0;

       /** Checks whether this set is empty.
    */
       virtual bool isEmpty() const = 0;

       /** Adds a new entry to this set.
    */
       virtual bool add(const ItemType& newEntry) = 0;
       /** Removes a given entry from this set,if possible.

    */
       virtual bool remove(const ItemType& anEntry) = 0;

       /** Removes all entries from this set.
    */
       virtual void clear() = 0;

       /** Tests whether this set contains a given entry.
    */
       virtual bool contains(const ItemType& anEntry) const = 0;

       /** Fills a vector with all entries that are in this set.
    */
       virtual std::vector<ItemType> toVector() const = 0; 

    }; // end SetInterface

    #endif /* SET_INTERFACE_H_ */

设置.h

#ifndef Set_H_

#define SET_H_
#include "SetInterface.h"

template <class ItemType> 
class Set: public SetInterface<ItemType>{
    public:
    Set();
        //Overriding SetInterface functions

    int getCurrentSize();

    bool isEmpty();

    bool add(const ItemType& newEntry);

    bool remove(const ItemType& anEntry);

    void clear();

    bool contains(const ItemType& anEntry);

    std::vector<ItemType> toVector();

    private:
    static const int DEFAULT_SET_SIZE = 4; // for testing purposes we will keep the set small

    ItemType items_[DEFAULT_SET_SIZE]; // array of set items

    int item_count_;                  // current count of set items

    int max_items_;    // max capacity of the set

    // post: Either returns the index of target in the array items_
    // or -1 if the array does not contain the target
    int getIndexOf(const ItemType& target) const;
};

#endif /*SET_H_*/

最后是 Playlist.h

#ifndef PLAYLIST_H_
#define PLAYLIST_H_
class PlayList: public Set<Song>{//inherits Set with Song replacing ItemType
    public:
    PlayList();
    PlayList(const Song& a_song);

    private:
    Set<Song> playlist_;//error: cannot declare field 'PlayList::playlist_' to be of abstract type 'Set<Song>'
};

#endif /*PLAYLIST_H_*/  

Set.h 和 PlayList.h 是通过它们各自的 cpp 文件定义的,但这似乎是我如何实现 playList 类的问题。据我了解,Set 模板类定义了 SetInterface 类中的所有虚函数(通过 Set.cpp 文件),没有问题,但我仍然不能声明 Set 对象吗?我很茫然。

非常感谢您一如既往的帮助。

最佳答案

这就是 C++11 引入 override 关键字的原因。您没有覆盖您的方法,因为缺少指定的 const

在派生类中添加:

std::vector<ItemType> toVector() override;

并看到错误。然后更改为:

std::vector<ItemType> toVector() const override;

再看看。将 const 添加到所有需要的方法中。如果您在基类中有 const - 合格成员,您也需要在派生类中提供 const

关于c++ - 无法将字段声明为嵌套模板中的抽象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52399408/

相关文章:

c++ - 你能制作一个接受 C++ 中的 vector 和数组的模板吗?

c++ - 初始化宽字符数组

c++ - 为什么我不能在类定义中实例化同一类的对象

java - Android onLocationChanged 和 MainActivity 类

c++ - 根据枚举成员的值特化模板

c++ - 使用可变参数模板帮助程序进行多个 std::variant 访问

c++ - C++ 中的列式文本抓取

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

java - Jsoup 获取多个类的 li

ios - Swift 2/XCode 7 - 在另一个类中调用 "EXC_BAD_ACCESS (code=2...)"时为 "ViewController().view"