私有/受保护的变量"error: within this context"
Private/Protected variable "error: within this context"
我有一个基于SFML gamefromscratch.com教程的类,称为"VisibleGameObject"在这个类中,是一个私有变量"_sprite",以及一个"getSprite()"函数,我尝试作为保护和公共。(即使作为公共函数,它仍然说"_sprite"是私有的,即使公共函数返回变量)。
在我的OnRender类中,我创建了两个VisibleGameObjects。
VisibleGameObject _testtile1;
VisibleGameObject _cursorSprite;
但是当我绘制精灵时,我得到了错误:within this context.
_mainWindow.draw(_cursorSprite._sprite);
或者我尝试(与getSprite()被保护或公共)。
_mainWindow.draw(_cursorSprite.getSprite());
然而,"error: 'sf::Sprite VisibleGameObject::_sprite'总是私有的。错误:within this context"
对我来说没有任何意义,因为
1) _sprite是VisibleGameObject的一个变量。它可能是私有的,但除了它自己的原始类"VisibleGameObject"之外,它不能被任何东西访问。我认为类可以访问自己的变量,即使它们是另一个类中的新实例化对象?
2) getSprite()是公共的,并返回私有变量,但它仍然说_sprite是私有的?这对我来说毫无意义!我学到的关于Getter和setter的所有知识都表明,公共函数可以返回私有变量,因为这就是Getter的全部意义。
sf::Sprite& VisibleGameObject::getSprite()
{
return _sprite;
}
class VisibleGameObject
{
public:
VisibleGameObject();
virtual ~VisibleGameObject();
private:
sf::Sprite _sprite;
protected:
sf::Sprite& getSprite();
或
public:
sf::Sprite& getSprite();
类的受保护成员只能被类本身和从它派生的类访问。
因为你调用绘制函数不是从类中派生自VisibleGameObject你得到一个错误。
你可能应该看看这个:http://www.cplusplus.com/doc/tutorial/inheritance/
- 表示"accepting anything for this template argument" C++概念的通配符
- 为什么使用 "this" 指针调用派生成员函数?
- C++错误:"error: int aaa::bbb is protected within this context"
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 关于C++中具有多重继承"this"指针的说明
- 在noexcept 规范中是否允许使用"this"?
- 如何修复"error: ‘_1’ was not declared in this scope"?
- C++调用具有 *this 属性的单个帮助程序函数
- Doees the 'this' 指针参与虚函数的多态行为
- 在 c++ 中正确定义"this"关键字?
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- "Attribute is protected within this context"继承和 .h 和.cpp文件
- "is private within this context"被抛出用于不应是私有的函数(GCC 5.3.0,C++11)
- "error: within this context" 在Qt 5中安装QFtp
- 私有/受保护的变量"error: within this context"
- C++ gcc和Visual Studio的不同编译错误,'within this context'
- G++ 编译错误"... is protected from within this context"而 clang 没有错误