У меня есть 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 {.
Я видел потоки с похожей проблемой и попробовал следующие предложения:
- Проверен, чтобы убедиться, что мои охранники включения были написаны правильно
- Включение моих файлов в .cpp вместо файла .hpp
- Включение класса вместо использования
#include "className.h"
- Использование циклического включения
Однако, я все еще получаю ту же ошибку, или она появляется для другого файла.Итак, я решил создать свой собственный пост.Вот код, который у меня есть для каждого файла:
SetInterface.h
#ifndef SET_INTERFACE_H_
#define SET_INTERFACE_H_
#include <vector>
template<class ItemType>
class SetInterface
{
public:
...
}; // end SetfInterface
#endif /* SET_INTERFACE_H_ */
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
Set.cpp
#include "Set.h"
#include "Song.h"
template<class ItemType>
class Set : SetInterface {
public:
...
};
Song.h
#include <string>
class Song {
public:
...
};
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_;
}
...
}
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_;
}
PlayList.cpp
#include "Set.h"
#include "Song.h"
#include "PlayList.h"
#include <iostream>
template<class ItemType>
class PlayList : public Set {
public:
...
}
Как исправить эту ошибку?