Jose Manuel Palau
Jose Manuel Palau

Reputation: 31

"Attribute is protected within this context" with inheritance and .h and .cpp files

I'm trying to code a game with Irrlicht, but I got stuck with this problem meanwhile I was separating the code into .h and .cpp files.

The main error in the compiler is 'node is protected within this context". Node is an attribute of "GameObjectOverworld", and was invoked from "Player" (GameObjectOverworld child class)

This worked fine until I separated the code in .h and .cpp files.

The 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

The player.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

And player.cpp (the one which sends the error)

#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);
    }

}

Upvotes: 0

Views: 856

Answers (1)

Tostifrosti
Tostifrosti

Reputation: 151

You forgot to link the method addPlayerModel to the class:

Change:

void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux)

To

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

Also change Player::node to this->node because your 'node' attribute is not static.

Upvotes: 3

Related Questions