Reputation:
I have a class based off of the SFML gamefromscratch.com tutorials, called "VisibleGameObject" Within this class, is a private variable "_sprite", as well as a "getSprite()" function that I tried as both protected and public. (Even as public, it still says "_sprite" is private even though the public function returns the variable).
In my OnRender class, I create two VisibleGameObjects.
VisibleGameObject _testtile1;
VisibleGameObject _cursorSprite;
But when I draw to draw the sprites, I get the error: within this context.
_mainWindow.draw(_cursorSprite._sprite);
alternatively I tried (with getSprite() being protected or public).
_mainWindow.draw(_cursorSprite.getSprite());
Yet always, "error: 'sf::Sprite VisibleGameObject::_sprite' is private. error: within this context"
Doesn't make any sense to me, because
1) _sprite is a variable of VisibleGameObject. It may be private, but it is not being accessed by anything but its own original class of "VisibleGameObject". I thought classes could access their own variables, even when they're a new instantiated object in another class?
2) getSprite() is public, and returns the private variable, yet it is STILL saying _sprite is private? This makes no sense to me! Everything I have learned about Getters and Setters, says that the public function CAN return a private variable, as that is the whole point of this Getter.
sf::Sprite& VisibleGameObject::getSprite()
{
return _sprite;
}
class VisibleGameObject
{
public:
VisibleGameObject();
virtual ~VisibleGameObject();
private:
sf::Sprite _sprite;
protected:
sf::Sprite& getSprite();
OR
public:
sf::Sprite& getSprite();
Upvotes: 0
Views: 19046
Reputation: 2000
Protected members of a class can only be accessed by the class itself and classes which are derived from it.
Since you are calling the draw function not from within a class that was derived from VisibleGameObject you get an error.
You propably should read this: http://www.cplusplus.com/doc/tutorial/inheritance/
Upvotes: 2