这个私有变量如何“未在此范围内声明”?
我目前正在尝试更多地了解 C++ 中的面向对象设计(熟悉 Java),但遇到了一些困难。我正在尝试将这个项目放在一起,以在使用 SFML 构建图形和音频的游戏中学习这些原理。我有以下两个文件。
WorldObject.h
#ifndef WORLDOBJECT_H
#define WORLDOBJECT_H
#include <SFML/Graphics.hpp>
#include <string>
#include "ImageManager.h"
class WorldObject
{
private:
sf::Sprite _sprite;
void SetImagePath(std::string path);
sf::Sprite GetGraphic();
};
#endif
WorldObject.cpp
#include "WorldObject.h"
void WorldObject::SetImagePath(std::string path)
{
_sprite.SetImage(*gImageManager.getResource(path));
}
sf::Sprite GetGraphic()
{
return _sprite;
}
我没有发现其中任何一个有任何问题,但是当我尝试编译它们时,我收到来自 g++ 的以下错误:
WorldObject.cpp: In function ‘sf::Sprite GetGraphic()’:
WorldObject.cpp:9: error: ‘_sprite’ was not declared in this scope
make: *** [WorldObject.o] Error 1
What am我在这段代码中丢失了?到目前为止,尝试理解设置继承层次结构的正确方法一直是游戏开发中造成最多问题的原因,但我知道这主要是由于我更习惯于使用 Java 的继承模型而不是 C++ 的多重继承模型。继承模型。
I'm currently trying to learn more about object oriented design in C++ (familiar with Java) and am running into some walls. The project I am trying to put together to learn these principles in a game built using SFML for the graphics and audio. I have the following two files.
WorldObject.h
#ifndef WORLDOBJECT_H
#define WORLDOBJECT_H
#include <SFML/Graphics.hpp>
#include <string>
#include "ImageManager.h"
class WorldObject
{
private:
sf::Sprite _sprite;
void SetImagePath(std::string path);
sf::Sprite GetGraphic();
};
#endif
WorldObject.cpp
#include "WorldObject.h"
void WorldObject::SetImagePath(std::string path)
{
_sprite.SetImage(*gImageManager.getResource(path));
}
sf::Sprite GetGraphic()
{
return _sprite;
}
I don't see any problem with either of these, and yet when I attempt to compile them I receive the following error from g++:
WorldObject.cpp: In function ‘sf::Sprite GetGraphic()’:
WorldObject.cpp:9: error: ‘_sprite’ was not declared in this scope
make: *** [WorldObject.o] Error 1
What am I missing in this code? Trying to understand the proper way to setup the inheritance hierarchy has been causing the most problems thus far in the game's development, but I know that that is primarily caused by the fact that I am more conditioned to using Java's inheritance model as opposed to C++'s multiple inheritance model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您在
WorldObject.cpp
中定义的函数GetGraphics
不是 WorldObject 类的成员。请注意
,如果从程序中的某个位置调用此函数,C++ 编译器只会抱怨缺少
WorldObject::GetGraphic
。The function
GetGraphics
that you define inWorldObject.cpp
is not a member of class WorldObject. Useinstead of
Note that the C++ compiler only complains about the missing
WorldObject::GetGraphic
if this function is called from somewhere in your program.sf::Sprite GetGraphic()
不正确,它声明了一个全局GetGraphic
函数。由于GetGraphic
是类WorldObject
的一个函数,所以它应该是sf::Sprite WorldObject::GetGraphic()
。sf::Sprite GetGraphic()
is not correct, it is declaring a globalGetGraphic
function . SinceGetGraphic
is a function of theclass WorldObject
it should besf::Sprite WorldObject::GetGraphic()
.我没有做过太多 C++,但我认为您需要在 WorldObject.cpp 中使用
WorldObject::GetGraphic
而不是GetGraphic
?I haven't done much C++ but I think you need
WorldObject::GetGraphic
instead ofGetGraphic
in WorldObject.cpp?我相信你的意思是:
sf::Sprite WorldObject::GetGraphic()
而不是
sf::Sprite GetGraphic()
WorldObject.cpp 中的
I believe you mean to have:
sf::Sprite WorldObject::GetGraphic()
not
sf::Sprite GetGraphic()
in WorldObject.cpp