Reputation: 11
I'm just starting out with OOP in C++ and I keep getting "base class undefined error". I'm coding a project that is basically a Library, but using classes. Google hasn't really been helping much because it seems that most of the time the error happened for other people was circular references in writing their own headers.
Here is the program(excuse the lengthiness, I crammed everything into one file for the time being) 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;
}
And here is the error log:
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
Upvotes: 0
Views: 2497
Reputation: 20842
C++ type's visibility extend from the point they are first declared forward. Order matters, it is an inherited trait of the C language, based on "textual" treatment of include files as opposed to true modules or multi file type metadata, where the compiler traditionally notes each type as it is declared syntactically, stores it in a symbol table, where it is immediately available.
This differs from Java or C# compilers which parse the whole source file first, then resolve types and symbols later, removing order from the equation.
To allow cyclical relationships in C++ there is a feature called a forward declaration. It allows you to declare pointer types to a type that hasn't yet been defined (note declared is different from defined).
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;
};
Upvotes: 0
Reputation: 6214
You need to declare (and in some cases, define) classes before they are used. This is just like variables, functions, and anything else in C++.
You should be able to fix the first three errors that the compiler is reporting by moving the definition of Borrowable
to the top of your file and putting Media
right after LibraryItem
.
The fourth error is because Newspaper
doesn't explicitly call a constructor for Media
, and Media
has no default constructor that the compiler can insert a call to.
Upvotes: 1