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

C++ error: object of abstract class type is not allowed

本文关键字:抽象类 类型 对象 错误 不允许 C++      更新时间:2023-10-16

继承有问题。我不知道剧本出了什么问题。。

在主

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;
};

文件采用列表.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;
};

采用列表.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    

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

有人能帮我吗?感谢

您似乎有拼写错误。

CSVDoglist中声明了名为displayAdoptionlist(包含小l)的函数,但在CSVDoglist中没有覆盖纯虚拟函数displayAdoptionList(包含大L)。