[英]Base class is undefined error (C2504)
我只是从C ++中的OOP开始,并且不断收到“基类未定义错误”。 我正在编写一个基本上是一个Library的项目,但是使用了类。 Google并没有真正帮上大忙,因为在大多数情况下,其他人发生的错误似乎是在编写自己的标头的循环引用。
这是driverLibrary.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
之后LibraryItem
。
第四个错误是因为Newspaper
没有显式调用Media
的构造函数,并且Media
没有编译器可以插入调用的默认构造函数。
C ++类型的可见性从最初声明它们的点开始。 顺序很重要,它是C语言的继承特性,基于对包含文件的“文本”处理,而不是真正的模块或多文件类型元数据,在这种情况下,编译器通常会在语法上声明每种类型,并将其存储在符号表,可立即使用。
这与Java或C#编译器不同,后者先解析整个源文件,然后解析类型和符号,从等式中消除顺序。
为了允许C ++中的循环关系,有一个称为前向声明的功能。 它允许您将指针类型声明为尚未定义的类型(声明的注释与已定义的注释不同)。
class A; // declaration
class B {
class A * a; // refers to A, but does not use any members within A yet
};
class A { // definition
int size;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.