我有一个ADT class Set
,它继承了其父模板class SetInterface
的方法。I also have
类Song and
类PlayList , which essentially inherits the
对应于{{1} }公众成员。我收到以下错误:
class Set
我看到了具有类似问题的线程,并尝试了以下建议:
In file included from Song.cpp:7:0: Set.h:12:33: error: expected class-name before ‘{’ token class Set : public SetInterface {.
但是,我仍然遇到相同的错误,或者对于其他文件它再次出现。因此,我决定创建自己的帖子。这是每个文件的代码:
SetInterface.h
#include "className.h"
Set.h
#ifndef SET_INTERFACE_H_
#define SET_INTERFACE_H_
#include <vector>
template<class ItemType>
class SetInterface
{
public:
...
}; // end SetfInterface
#endif /* SET_INTERFACE_H_ */
Set.cpp
#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
Song.h
#include "Set.h"
#include "Song.h"
template<class ItemType>
class Set : SetInterface {
public:
...
};
Song.cpp
#include <string>
class Song {
public:
...
};
播放列表.h
#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.cpp
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_;
}
如何纠正此错误?
答案 0 :(得分:1)
由于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