I've got the following C++ code in XCode, giving two errors I cannot make sense of:
#include <iostream>
class Buch{
public:
Buch (long int nummer = 0, char autor[25] = (char*)"", int jahr = 0, bool ausgel = false);
long int getNr();
int getJahr();
protected:
long int __nummer;
char __autor[25];
int __jahr;
bool __ausgel;
void setAusl(bool x);
bool getAusl();
};
class FachBuch : Buch {
public:
void setSWort(int sw);
int getSWort();
protected:
char __fach[15];
int __swort;
};
class UBuch : Buch {
public:
void setAlter(int a);
int getAlter();
protected:
int __kateg;
char __land[15];
private:
int _ab_alter;
};
class Bildband : UBuch {
public:
Bildband(long int nummer = 0, char autor[25] = (char*)"", int jahr = 0, int kategorie = 0, char land[15] = (char*)"");
};
Buch::Buch (long int nummer, char autor[25], int jahr, bool ausgel) {
__nummer = nummer;
//_autor = autor;
__jahr = jahr;
__ausgel = ausgel;
}
long int Buch::getNr() {
return __nummer;
}
int Buch::getJahr() {
return __jahr;
}
void Buch::setAusl(bool x) {
__ausgel = x;
}
bool Buch::getAusl() {
return __ausgel;
}
void FachBuch::setSWort(int x) {
__swort = x;
}
int FachBuch::getSWort() {
return __swort;
}
void UBuch::setAlter(int x) {
_ab_alter = x;
}
int UBuch::getAlter() {
return _ab_alter;
}
Bildband::Bildband(long int nummer, char autor[25], int jahr, int kategorie, char land[15]) {
__nummer = nummer; // error message: Cannot cast 'Bildband' to its private base class 'Buch'
//Buch(nummer, autor, jahr, false); // error message: '__nummer' is a private member of 'Buch'
}
int main () {
Bildband Masuren(356780, (char*)"Kranz", 2010, 4, (char*)"Polen");
return 0;
}
I get the following errors: main.cpp:92:5: Cannot cast 'Bildband' to its private base class 'Buch' main.cpp:92:5: '__nummer' is a private member of 'Buch'
My knowledge of C++ is quite limited and I had no luck googling, probably mainly because I lack the necessary C++ basic knowledge.
Can anyone explain to me why these errors occur and what terms I need to look up to understand the problem?
Thanks in advance.
They are not available because UBuch
inherits Buch
privately. When defining a class, inheritance is private by default.
// These two lines mean the same thing:
class UBuch : Buch
class UBuch : private Buch
All members of Buch
are inherited, but those visible to UBuch
are inherited as private to UBuch
.
This means that code external to UBuch
has no access to any members of Buch
on UBuch
objects, nor can it convert pointers or references to UBuch
objects to pointers or references to Buch
.
This includes classes that derive UBuch
.
class Bildband : UBuch
Even though Buch
is in the inheritance chain, its members were inherited privately by UBuch
, and so Bildband
has no access to the members inherited from Buch
.
To fix this, you should inherit Buch
publicly (and you probably want all of your other classes to inherit publicly from their respective base classes, too):
class UBuch : public Buch
Also, note that identifiers containing two consecutive underscores are reserved by the environment, so all such identifiers in your code (__nummer
, __autor
, etc.) cause undefined behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With