c++ - 基类未定义错误 (C2504)

标签 c++ class oop

我刚开始使用 C++ 中的 OOP,我不断收到“基类未定义错误”。我正在编写一个基本上是库但使用类的项目。 Google 并没有真正提供多少帮助,因为大多数情况下,其他人发生的错误似乎是在编写自己的 header 时出现循环引用。

这是程序(请原谅冗长,我暂时把所有东西都塞进了一个文件) 驱动程序库.cpp:

#include "stdafx.h"
#include <iostream>
using namespace std;

class LibraryItem :public Borrowable{
public:
    LibraryItem(string name, int id) :identifier(name), id(id){}
    LibraryItem();
    ~LibraryItem();
    string getIdentifier(){
        return identifier;
    }
    int getId(){
        return id;
    }
    bool borrow(){ return false; }
    bool canBorrow() { return false; }
    bool operator== (LibraryItem &comparison)
    {
        return (comparison.getId() == this->id);
    }
protected:
    string identifier;
    int id;
};
class Borrowable{//interface
public:
    bool isIn;
    virtual bool borrow() = 0;
    virtual bool canBorrow() = 0;
};
class Book :public LibraryItem, public Borrowable{ //all children will be borrowed the same way unless specified in their own class
public:
    Book(string name, int id, string author, string genre) :author(author), genre(genre){ LibraryItem(name, id); }
    Book(string name, int id, string author){ LibraryItem(name, id); this->author = author; }
    string getAuthor(){ return author; }
    string getGenre(){ return genre; }
    //override
    bool borrow(){
        if (!isIn) return false;
        else isIn = false;
        return true;
    }
    bool canBorrow(){ return isIn; }
protected:
    string author;
    string genre;
    bool isIn;//override
};
class Newspaper : public Media{
public:
    Newspaper(string name, int id, int vol, int issue) : vol(vol), issue(issue){ LibraryItem(name, id); }
    int getVol(){ return vol; }
    int getIssue(){ return issue; }
    bool borrow(){
        if (!isIn) return false;
        else isIn = false;
        return true;
    }
    bool canBorrow(){ return isIn; }
protected:
    int vol;
    int issue;
    bool isIn;
};
class Reference : public Book{//, public Borrowable{
public:
    Reference(string name, int id, string author, int vol, int issue, string topic) : vol(vol), issue(issue), topic(topic), Book(name, id, author){}
    int getVol(){ return vol; }
    int getIssue(){ return issue; }
    //  bool borrow(){ return false; }
    //  bool canBorrow(){ return false; }
protected:
    int vol;
    int issue;
    string topic;
};
class Magazine : public Media, public Borrowable{
public:
    Magazine(string name, int id, string title, string publisher, int date);
};
class Media : public LibraryItem, public Borrowable{
public:
    Media(string name, int id, string title, string publisher) : title(title), publisher(publisher), LibraryItem(name, id){}
    bool borrow(){
        if (!isIn) return false;
        else isIn = false;
        return true;
    }
    bool canBorrow(){ return isIn; }
protected:
    string title;
    string publisher;
    bool isIn;
};
class CD :public Media{
public:
    CD(string name, int id, string title, string publisher, int length, string artist) :length(length), artist(artist), Media(name, id, title, publisher) {}
    int getLength(){ return length; }
    string getArtist(){ return artist; }
protected:
    int length;
    string artist;
};
class DVD : public Media{
public:
    DVD(string name, int id, string title, string publisher, int dpi) : dpi(dpi), Media(name, id, title, publisher) {}
    int getDPI(){ return dpi; }
protected:
    int dpi;
};

int main()
{
    Book book = Book("Identifier", 234, "Mike Hunt");
    return 0;
}

这是错误日志:

Error   1   error C2504: 'Borrowable' : base class undefined    c:\users\connor\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\driverlibrary.cpp 8   1   ConsoleApplication2
Error   2   error C2504: 'Media' : base class undefined c:\users\connor\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\driverlibrary.cpp 53  1   ConsoleApplication2
Error   3   error C2504: 'Media' : base class undefined c:\users\connor\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\driverlibrary.cpp 81  1   ConsoleApplication2
    4   IntelliSense: no default constructor exists for class "Media"   c:\Users\Connor\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\driverLibrary.cpp 55  77  ConsoleApplication2

最佳答案

您需要在使用类之前声明(在某些情况下,定义)类。这就像 C++ 中的变量、函数和其他任何东西。

您应该能够修复编译器报告的前三个错误,方法是将 Borrowable 的定义移动到文件的顶部并将 Media 紧跟在 图书馆项目

第四个错误是因为 Newspaper 没有显式调用 Media 的构造函数,并且 Media 没有编译器可以调用的默认构造函数插入一个调用。

关于c++ - 基类未定义错误 (C2504),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24947179/

相关文章:

c++ - 从 cv::Mat 初始化的 IplImage 的内存释放

c# - 在 c# 中,如何选择命名空间和类使用的命名约定?

java - 在面向对象编程 Java 中使用数组和方法

python - 设置属于某个类的类的最有效方法是什么

java - 我们怎样才能摆脱不必要的继承呢?

c++ - 如何用 gcc 抑制 boost::thread 警告?

c++ - 奇怪的 std::sort 行为

c++ - QThread 中的 sleep 函数

c++ - (复制构造函数)作为参数传递给另一个对象的对象如何访问私有(private)成员?

python - 无法使用基类创建其子类的实例