C ++错误:不允许使用抽象类类型的对象

时间:2016-04-17 15:32:31

标签: c++

继承有问题。我不知道脚本有什么问题..

中的

int main(){
    Repository repo("dogs.txt");
    FileAdoptionList* a = new CSVDoglist{}; / here is the error
    Controller ctrl(repo, dogValidator{}, a);
    UI ui(ctrl);

    ui.startUI();

    delete a;
}

CSVDoglist.h

class CSVDoglist : public FileAdoptionList
{
public:

    void writeToFile();
    void displayAdoptionlist() const;
};

FileAdoptionList.h

class FileAdoptionList : public AdoptionList
{
protected:
    std::string filename;

public:
    FileAdoptionList();
    virtual ~FileAdoptionList() {}

    void setFilename(const std::string& filename);
    virtual void writeToFile() = 0;
    virtual void displayAdoptionList() const = 0;
};

AdoptionList.h

class AdoptionList
{
protected:
    std::vector<Dog> storage;

public:
    AdoptionList();

    // Adds a dog to the playlist.
    void add(const Dog& dog);
    // Checks if the adoptionlist is empty.
    bool isEmpty();

    virtual ~AdoptionList() {}
};

错误:

object of abstract class type "CSVDoglist" is not allowed:  

'CSVDoglist': cannot instantiate abstract class Adoptig Dogs    

我已经阅读了有关此问题的更多主题,但我没有找到解决方案。

有人能帮助我吗?感谢

1 个答案:

答案 0 :(得分:3)

好像你有一个错字。

名为displayAdoptionlist的函数(包含小l)在CSVDoglist中声明,但纯虚函数displayAdoptionList(包含大L)isn&# 39; t覆盖在CSVDoglist