【问题标题】:"Attribute is protected within this context" with inheritance and .h and .cpp files“属性在此上下文中受保护”具有继承和 .h 和 .cpp 文件
【发布时间】:2023-04-05 20:43:01
【问题描述】:

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

编译器中的主要错误是“节点在此上下文中受到保护”。 Node是“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);
    }

}

【问题讨论】:

  • 六个using namespace 声明?哇...
  • 我正在努力适应 Irrlicht,但是是的,很多

标签: c++ file inheritance


【解决方案1】:

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

变化:

void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux)

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

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

【讨论】:

猜你喜欢
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
相关资源
最近更新 更多