c++ - "Attribute is protected within this context"具有继承和 .h 和 .cpp 文件

标签 c++ file inheritance

我正在尝试使用 Irrlicht 编写游戏代码,但在将代码分成 .h 和 .cpp 文件时遇到了这个问题。

编译器中的主要错误是“节点在此上下文中受到保护”。 节点是“GameObjectOverworld”的属性,并从“Player”(GameObjectOverworld 子类)调用

在我将 .h 和 .cpp 文件中的代码分开之前,这工作正常。

GameObjectOverworld.h

#ifndef __GAMEOBJECTOVERWORLD_H__
#define __GAMEOBJECTOVERWORLD_H__

#include <irrlicht.h>
#include <stdio.h>
#include "GameObject.h"

class GameObjectOverWorld : public GameObject{
    protected:
        scene::ISceneNode* node = nullptr; 
    public: 
        GameObjectOverWorld() {}
        core::vector3df getPosition(){return node->getPosition();}
};

#endif

播放器.h

#ifndef __PLAYER_H__
#define __PLAYER_H__

#include <irrlicht.h>
#include <stdio.h>
#include "GameObject.h"
#include "GameObjectOverworld.h"

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class Player : public GameObjectOverWorld{
    private: 

        std::string name =""; 
        float speed = 15.0f; 
    public:
        Player() = default;
        void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux){}
        void move (char axis, int direction, float frameDeltaTime){}
};
#endif

和 player.cpp(发送错误的那个)

#include "Player.h"

void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux){
    Player::node = smgraux->addCubeSceneNode(10.0f, 0, 0, core::vector3df(15.0f, 0.0f, 45.0f), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));

    if (node)
    {

        node->setMaterialTexture(0, driveraux->getTexture("Materials/madero.jpg"));
        node->setMaterialFlag(video::EMF_LIGHTING, false);
    }

}

最佳答案

您忘记将方法 addPlayerModel 链接到类:

改变:

void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux)

void Player::addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux)

还要将 Player::node 更改为 this->node,因为您的“节点”属性不是静态的。

关于c++ - "Attribute is protected within this context"具有继承和 .h 和 .cpp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49210604/

相关文章:

java - 如何从 jni 代码 junit 测试回调

java - JVM 堆栈、堆和线程如何映射到物理内存或操作系统

c++ - 有效的 C++ 仍然有效吗?

c++ - 如何在指向基类对象的指针 vector 中引用派生对象?

c++ - 在派生类中调用 sizeof(*this)

c++ - 如何在 AVX2 中将 32 位无符号整数转换为 16 位无符号整数?

java - 将本地目录中的 zip 文件放入 java spring boot 中的列表中

Android从Uri获取文件的正确方法

python - python中压缩文件夹的完整文件路径

java - 了解Java父类(super class)和子类构造函数和方法之间的关系