Trying to access protected member from a derived class.

Hi,

I have declared an std list, set it as protected and am trying to use it in a derived class, code:

///////////////////////////////////////////////////////
bool Player2:: Collision(Player& pos1)
{

for(auto i = positions_.begin() ; i != positions_.end(); i++)
{
if( i == positions_.begin() )
{
i++;
}

if(positions_.begin() == pos1.positions_.front().x &&
positions_.begin() == pos1.positions_.front().y)
{

return true;

}
}

}
/////////////////////////////////////////////////////////

However I keep getting this error 'std::list<Position>Player::positions_' is protected.


My Player2.cpp file
/////////////////////////////////////////////////////////////////////
class Player2 : public Player
{

public:
Player2();
bool onKey( const KeyEvent& key_event );
bool Collision(Player& pos1);

};
/////////////////////////////////////////////////////////////////


positions_ is indeed set as protected in the Player class making me able to access it as far as I know.

Any suggestions?



P.S new to the website
Last edited on
1
2
3
4
5
6
7
8
9
10
11
class foo{
protected:
	int value;
};

class bar: public foo{
public:
	void zzz(foo &a){
		a.value = 42;
	}
};
foo.cpp:9:5: error: 'value' is a protected member of 'foo'
                a.value = 42;
                  ^
foo.cpp:3:6: note: can only access this member on an object of type 'bar'
        int value;
            ^
1 error generated.

It would work if the function was void zzz(bar&);
I don't know the rationale behind this decision.


PS: when posting code, use code tags [code][/code]
Topic archived. No new replies allowed.