Getting error variable is inaccessible : <p>I've been writing some code for a small console based game that features a player and some enemies. So far I have only written one of the methods for my player class.
The player class is a child class of a parent class called entity that also has a child class called enemy. In a method of player.cpp, <code>void player::takeInput()</code>, I attempt to modify the value <code>this-&gt;xCoordinate</code> where <code>int xCoordinate</code> is a public member of the entity class. Whenever I compile this code, I am given the error:</p>

<pre><code>include\entity.h line 31 error: 'int entity::xCoordinate' is inaccessible
C:\Users....\ line 72 error:  'error within this context'
</code></pre>

<p>Note that on line 72 the code is:</p>

<pre><code>int newX = this-&gt;xCoordinate;
</code></pre>

<p>This error is repeated for all of the other (many) calls I make to <code>this-&gt;xCoordinate</code> or <code>this-&gt;yCoordinate</code> each time with the error on line 31 of entity.h and the error: 'error within this context'.</p>

<p>I think that this error is something to do with how the classes inherit from each other. The only solution I was able to find, was addressing a problem where the member was labelled private, which doesn't apply to this issue as I have labelled my member as public.</p>

<p>I'm not a super experienced programmer and would only rate myself as intermediate, so please excuse the code if it contains more than a few mistakes!</p>

<p>Thanks in advance for any help!
Here's the source code, note that I haven't done too much work on the rest of the project yet:</p>

<p>main.cpp:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include "entity.h"
#include "player.h"
#include "enemy.h"
#include &lt;vector&gt;

using namespace std;

int playerX;
int playerY;
int noEnemies;

void initGrid()
{
    return;
}

void dispGrid()
{

    return;
}

int verifyMove(int x, int y)
{

    return 0;
}

void aiTurn()
{
    return;
}

int main()
{

    return 0;
}
</code></pre>

<p>entity.h:</p>

<pre><code>#ifndef ENTITY_H
#define ENTITY_H
#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;
enum direction
{
    NORTH,
    SOUTH,
    EAST,
    WEST
};

enum weaponClass
{
    PISTOL,
    SWORD,
    RIFLE,
    SHOTGUN
};



class entity
{
    public:
        weaponClass currentWeapon;
        int health;
        int weaponStrength;
        int xCoordinate;
        int yCoordinate;
        string spritePath;
        string name;
    protected:

    private:
};

#endif // ENTITY_H
</code></pre>

<p>entity.cpp contains no code as it has no methods. It is merely used as a grouping for enemy.h and player.h</p>

<p>player.h:</p>

<pre><code>#ifndef PLAYER_H
#define PLAYER_H

#include "entity.h"
#include &lt;iostream&gt;



class player : public entity
{
    public:
        direction nextMoveDir;
        direction attackDir;
        int ammunition;
        player();
        virtual ~player();
        virtual void addToGrid();
        virtual void draw();
        void takeInput();
        void restoreHealth();
        void makeMove();
        int health;

    protected:

    private:
};

#endif // PLAYER_H
</code></pre>

<p>player.cpp: (sorry for the huge amount of code!)</p>

<pre><code>#include "player.h"
#include "entity.h"
#include "enemy.h"

#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;

player::player()
{
    //ctor
}

player::~player()
{
    //dtor
}

vector&lt;enemy*&gt; listOfEntities; 



void player::takeInput()
{
    cout &lt;&lt; "Would you like to move (1), or attack (2)?" &lt;&lt; endl;
    int input;
    cin &gt;&gt; input;
    if(input == 1)
    {
        bool cont = false;
        while(!cont)
        {
            cont = true;
            // Moving
            cout &lt;&lt; "In which direction would you like to move:" &lt;&lt; endl;
            cout &lt;&lt; "1) North, 2) South, 3) East, 4) West." &lt;&lt; endl;
            int newX = this-&gt;xCoordinate;
            int newY = this-&gt;yCoordinate;
            cin &gt;&gt; input;
            switch(input)
            {
            case 1:
                this-&gt;nextMoveDir = NORTH;
                newX++;
                break;
            case 2:
                this-&gt;nextMoveDir = SOUTH;
                newX--;
                break;
            case 3:
                this-&gt;nextMoveDir = EAST;
                newY++;
                break;
            case 4:
                this-&gt;nextMoveDir = WEST;
                newY--;
                break;
            default:
                cont = false;
                cout &lt;&lt; "Invalid selection." &lt;&lt; endl;
                break;

            }
            for(int i = 0; i &lt; listOfEntities.size(); i++)
            {
                int itrX = listOfEntities[i]-&gt;xCoordinate;
                int itrY = listOfEntities[i]-&gt;yCoordinate;
                if(((newX == this-&gt;xCoordinate) &amp;&amp; (newY = this-&gt;yCoordinate))) )
                {
                    // tile is occupied
                    cout &lt;&lt; "That tile is occupied!" &lt;&lt; endl;
                    cont = false;
                    break;
                }
            }
        }


    }
    else if(input == 2)
    {
        // Attacking
        bool cont = false;
        while(!cont)
        {
            cont = true;

            switch(this-&gt;currentWeapon)
            {
            case SWORD:
                cout &lt;&lt; "Your current weapon is a sword that has a range of 1 and a damage rating of 5." &lt;&lt; endl;
                cout &lt;&lt; "In which direction do you wish to attack:" &lt;&lt; endl;
                cout &lt;&lt; "1) North, 2) South, 3) East, 4) West." &lt;&lt; endl;
                int input;
                cin &gt;&gt; input;
                enemy *enemyToAttack;
                switch(input)
                {
                case 1:
                    this-&gt;attackDir = NORTH;
                    for(int i = 0; i &lt; listOfEntities.size(); i++)
                    {
                        int xCor = listOfEntities[i]-&gt;xCoordinate;
                        int yCor = listOfEntities[i]-&gt;yCoordinate;
                        if((xCor == this-&gt;xCoordinate) &amp;&amp; (xCor = this-&gt;yCoordinate+1))
                        {
                            enemyToAttack = listOfEntities[i];
                        }

                    }
                    break;
                case 2:
                    this-&gt;attackDir = SOUTH;
                    for(int i = 0; i &lt; listOfEntities.size(); i++)
                    {
                        int xCor = listOfEntities[i]-&gt;xCoordinate;
                        int yCor = listOfEntities[i]-&gt;yCoordinate;
                        if((xCor == this-&gt;xCoordinate) &amp;&amp; (xCor = this-&gt;yCoordinate-1))
                        {
                            enemyToAttack = listOfEntities[i];
                        }

                    }
                    break;
                case 3:
                    this-&gt;attackDir = EAST;
                    for(int i = 0; i &lt; listOfEntities.size(); i++)
                    {
                        int xCor = listOfEntities[i]-&gt;xCoordinate;
                        int yCor = listOfEntities[i]-&gt;yCoordinate;
                        if((xCor == this-&gt;xCoordinate+1) &amp;&amp; (xCor = this-&gt;yCoordinate))
                        {
                            enemyToAttack = listOfEntities[i];
                        }

                    }
                    break;
                case 4:
                    this-&gt;attackDir = WEST;
                    for(int i = 0; i &lt; listOfEntities.size(); i++)
                    {
                        int xCor = listOfEntities[i]-&gt;xCoordinate;
                        int yCor = listOfEntities[i]-&gt;yCoordinate;
                        if((xCor == this-&gt;xCoordinate-1) &amp;&amp; (xCor = this-&gt;yCoordinate))
                        {
                            enemyToAttack = listOfEntities[i];
                        }

                    }
                    break;
                default:
                    cont = false;
                    cout &lt;&lt; "Invalid selection." &lt;&lt; endl;
                    break;
                }
                if(enemyToAttack == NULL)
                {
                    player::takeInput();
                }
                enemyToAttack-&gt;health = health - 5;
                cout &lt;&lt; "You attack the enemy for 5 damage!" &lt;&lt; endl;
                cout &lt;&lt; "They are now on " &lt;&lt; enemyToAttack-&gt;health &lt;&lt; "HP." &lt;&lt; endl;
                break;
            case PISTOL:
                cout &lt;&lt; "Your current weapon is a pistol that has a range of 4 and a damage rating of 3 with " &lt;&lt; this-&gt;ammunition &lt;&lt; " bullets remaining." &lt;&lt; endl;
                cout &lt;&lt; "In which direction do you wish to attack:" &lt;&lt; endl;
                cout &lt;&lt; "1) North, 2) South, 3) East, 4) West." &lt;&lt; endl;
                cin &gt;&gt; input;
                switch(input)
                {
                case 1:
                    this-&gt;attackDir = NORTH;
                    for(int i = 0; i &lt; 4; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate) &amp;&amp; (xCor = this-&gt;yCoordinate+i))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                case 2:
                    this-&gt;attackDir = SOUTH;
                    for(int i = 0; i &lt; 4; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate) &amp;&amp; (xCor = this-&gt;yCoordinate-i))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                case 3:
                    this-&gt;attackDir = EAST;
                    for(int i = 0; i &lt; 4; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate+i) &amp;&amp; (xCor = this-&gt;yCoordinate))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                case 4:
                    this-&gt;attackDir = WEST;
                    for(int i = 0; i &lt; 4; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate-i) &amp;&amp; (xCor = this-&gt;yCoordinate))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                default:
                    cont = false;
                    cout &lt;&lt; "Invalid selection." &lt;&lt; endl;
                    break;
                }
                if(enemyToAttack == NULL)
                {
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                }
                enemyToAttack-&gt;health = health - 3;
                cout &lt;&lt; "You attack the enemy for 3 damage!" &lt;&lt; endl;
                cout &lt;&lt; "They are now on " &lt;&lt; enemyToAttack-&gt;health &lt;&lt; "HP." &lt;&lt; endl;
                break;
            case RIFLE:
                cout &lt;&lt; "Your current weapon is a rifle that has a range of 10 and a damage rating of 6 with " &lt;&lt; this-&gt;ammunition &lt;&lt; " bullets remaining." &lt;&lt; endl;
                cout &lt;&lt; "In which direction do you wish to attack:" &lt;&lt; endl;
                cout &lt;&lt; "1) North, 2) South, 3) East, 4) West." &lt;&lt; endl;
                cin &gt;&gt; input;
                switch(input)
                {
                case 1:
                    this-&gt;attackDir = NORTH;
                    for(int i = 0; i &lt; 10; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate) &amp;&amp; (xCor = this-&gt;yCoordinate+1))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                case 2:
                    this-&gt;attackDir = SOUTH;
                    for(int i = 0; i &lt; 10; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate) &amp;&amp; (xCor = this-&gt;yCoordinate+1))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                case 3:
                    this-&gt;attackDir = EAST;
                    for(int i = 0; i &lt; 10; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate+i) &amp;&amp; (xCor = this-&gt;yCoordinate))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                // Confirm that there is nothing in the way of the move.se 4:
                    this-&gt;attackDir = WEST;
                    for(int i = 0; i &lt; 10; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate-i) &amp;&amp; (xCor = this-&gt;yCoordinate))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                default:
                    cont = false;
                    cout &lt;&lt; "Invalid selection." &lt;&lt; endl;
                    break;
                }
                if(enemyToAttack == NULL)
                {
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                }
                enemyToAttack-&gt;health = health - 6;
                cout &lt;&lt; "You attack the enemy for 6 damage!" &lt;&lt; endl;
                cout &lt;&lt; "They are now on " &lt;&lt; enemyToAttack-&gt;health &lt;&lt; "HP." &lt;&lt; endl;
                break;
            case SHOTGUN:
                cout &lt;&lt; "Your current weapon is a shotgun that has a range of 2 and a damage rating of 10 with " &lt;&lt; this-&gt;ammunition &lt;&lt; " cartridges remaining." &lt;&lt; endl;
                cout &lt;&lt; "In which direction do you wish to attack:" &lt;&lt; endl;
                cout &lt;&lt; "1) North, 2) South, 3) East, 4) West." &lt;&lt; endl;
                cin &gt;&gt; input;
                switch(input)
                {
                case 1:
                    this-&gt;attackDir = NORTH;
                   for(int i = 0; i &lt; 2; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate) &amp;&amp; (xCor = this-&gt;yCoordinate+1))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                case 2:
                    this-&gt;attackDir = SOUTH;
                    for(int i = 0; i &lt; 2; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate) &amp;&amp; (xCor = this-&gt;yCoordinate-1))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                case 3:
                    this-&gt;attackDir = EAST;
                    for(int i = 0; i &lt; 2; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinate+1) &amp;&amp; (xCor = this-&gt;yCoordinate))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                case 4:
                    this-&gt;attackDir = WEST;
                    for(int i = 0; i &lt; 2; i++)
                    {
                        for(int j = 0; j &lt; listOfEntities.size(); j++)
                        {
                            int xCor = listOfEntities[j]-&gt;xCoordinate;
                            int yCor = listOfEntities[j]-&gt;yCoordinate;
                            if((xCor == this-&gt;xCoordinat-1) &amp;&amp; (xCor = this-&gt;yCoordinate))
                            {
                                enemyToAttack = listOfEntities[j];
                            }

                        }
                        break;
                    }
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                default:
                    cont = false;
                    cout &lt;&lt; "Invalid selection." &lt;&lt; endl;
                    break;
                }
                if(enemyToAttack == NULL)
                {
                    cout &lt;&lt; "Your attack missed!" &lt;&lt; endl;
                    break;
                }
                enemyToAttack-&gt;health = health - 10;
                cout &lt;&lt; "You attack the enemy for 5 damage!" &lt;&lt; endl;
                cout &lt;&lt; "They are now on " &lt;&lt; enemyToAttack-&gt;health &lt;&lt; "HP." &lt;&lt; endl;
                break;
            }
        }

    }
}
</code></pre>

<p>enemy.h:</p>

<pre><code>#ifndef ENEMY_H
#define ENEMY_H

#include "entity.h"
#include &lt;iostream&gt;

class enemy : entity
{
    public:
        enemy();
        virtual ~enemy();
        void attackPlayer();
        void upgradeHealth();
        void upgradeWeapons();
        int health;
    protected:

    private:
};

#endif // ENEMY_H
</code></pre>

<p>enemy.cpp has no code yet.
I am using the GNU Compiler with Code::Blocks
Thanks!</p>
