正如标题所说,我无法从主文件中访问基类的公共成员变量。
Entity.h:14:10:错误:'char Entity :: character'无法访问
entity.h:
#include "Vector2.h"
#include <Windows.h>
class Entity {
public:
Entity(char character, WORD color, Vector2<int> pos);
Vector2<int> pos;
Vector2<int> vel;
char character;
WORD color;
};
player.h:
#include "Entity.h"
class Player : Entity {
public:
Player(int lives, char character, WORD color, Vector2<int> pos);
int lives;
int points;
};
main中的构造函数调用:
int main(){
Player player(0, 'C', FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, playerSpawn);
char c = player.character; // error here
return 0;
}
有什么建议吗?
答案 0 :(得分:10)
继承类的默认访问级别是私有的,因此Player私下继承实体。
如果您想要公开访问,请使用public前面的类名。
class Player : public Entity {
...
};