我刚刚开始使用C ++中的OOP而且我不断得到"基类未定义错误"。我编写的项目基本上是一个库,但使用的是类。 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
答案 0 :(得分:1)
您需要在使用类之前声明(并在某些情况下定义)类。这就像C ++中的变量,函数和其他任何东西一样。
您应该能够通过将Borrowable
的定义移到文件顶部并将Media
放在LibraryItem
之后来修复编译器报告的前三个错误。< / p>
第四个错误是因为Newspaper
没有显式调用Media
的构造函数,Media
没有编译器可以插入调用的默认构造函数。
答案 1 :(得分:0)
C ++类型的可见性从它们首次向前声明的那一点开始扩展。顺序很重要,它是C语言的继承特征,基于&#34; textual&#34;处理包含文件而不是真正的模块或多文件类型元数据,其中编译器传统上记录每个类型,因为它在语法上被声明,将其存储在符号表中,它立即可用。
这与Java或C#编译器不同,Java编译器首先解析整个源文件,然后解析类型和符号,从等式中删除顺序。
为了在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;
};