我在XCode中有以下的C++代码,出现了两个我无法理解的错误:
#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;
}
我收到了以下错误信息: main.cpp:92:5: 无法将“Bildband”强制转换为其私有基类“Buch” main.cpp:92:5:“__nummer”是“Buch”的私有成员
我的C ++知识非常有限,我在谷歌上也没有找到答案,可能主要是因为我缺乏必要的C ++基础知识。
有人能解释一下这些错误发生的原因,并告诉我需要查找哪些术语来理解问题吗?
提前致谢。
protected
。 - Mooing Duckclass FachBuch : Buch
应该改为class FachBuch : public Buch
。 - cdhowiestruct FachBuch : Buch
。 - juanchopanza