C ++: ошибка: ожидаемое имя класса перед маркером '{' - PullRequest
0 голосов
/ 23 сентября 2018

У меня есть ADT class Set, который наследует методы своего родительского шаблона class SetInterface. I also have, класс Song and, класс PlayList , which essentially inherits the, соответствующий class Set открытым членам.Я получаю следующую ошибку:

In file included from Song.cpp:7:0: Set.h:12:33: error: expected class-name before ‘{’ token class Set : public SetInterface {.

Я видел потоки с похожей проблемой и попробовал следующие предложения:

  1. Проверен, чтобы убедиться, что мои охранники включения были написаны правильно
  2. Включение моих файлов в .cpp вместо файла .hpp
  3. Включение класса вместо использования #include "className.h"
  4. Использование циклического включения

Однако, я все еще получаю ту же ошибку, или она появляется для другого файла.Итак, я решил создать свой собственный пост.Вот код, который у меня есть для каждого файла:

  1. SetInterface.h

    #ifndef SET_INTERFACE_H_
    
    #define SET_INTERFACE_H_
    
    #include <vector>
    
    template<class ItemType>
    
    class SetInterface
    
    {
    
     public:
    ...
    }; // end SetfInterface
    
    #endif /* SET_INTERFACE_H_ */
    
  2. Set.h

    #ifndef SET_H_
    
    #define SET_H_
    
    template <class ItemType>
    class Set : public SetInterface {
    
    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
    
     int getIndexOf(const ItemType& target) const;
    
    };
    
    #endif
    
  3. Set.cpp

    #include "Set.h"
    #include "Song.h"
    
     template<class ItemType>
     class Set : SetInterface {
     public:
     ...
    };
    
  4. Song.h

    #include <string>
    
    class Song {
    
    public:
    ...
    };
    
  5. Song.cpp

    #include "Set.h"
    #include "Song.h"
    #include <string>
    #include <iostream>
    //Default constructor for Song which initializes values
    Song::Song() {
      std::string title_;
      std::string author_;
      std::string album_;
     }
    ...
    }
    
  6. Playlist.h

    class PlayList : public Set {
    public: 
    
      PlayList();
    
      PlayList(const Song& a_song);
      int getNumberOfSongs() const;
    
      bool isEmpty() const;
    
      bool addSong(const Song& new_song);
    
      bool removeSong(const Song& a_song);
    
      void clearPlayList();
    
      void displayPlayList() const;
    
     private: 
    
        Set<Song> playlist_;
    }
    
  7. PlayList.cpp

    #include "Set.h"
    #include "Song.h"
    #include "PlayList.h"
    #include <iostream>
    
    template<class ItemType>
    class PlayList : public Set {
    public:
    ...
    }
    

    Как исправить эту ошибку?

1 Ответ

0 голосов
/ 23 сентября 2018

Поскольку SetInterface является классом шаблона, вам необходимо указать параметр шаблона при наследовании от него:

#ifndef SET_H_

#define SET_H_

template <class ItemType>
class Set : public SetInterface<ItemType> {

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

 int getIndexOf(const ItemType& target) const;

};

#endif
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...