这个私有变量如何“未在此范围内声明”?

发布于 2024-10-12 14:54:20 字数 1029 浏览 10 评论 0原文

我目前正在尝试更多地了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彡翼 2024-10-19 14:54:20

您在 WorldObject.cpp 中定义的函数 GetGraphics 不是 WorldObject 类的成员。请

sf::Sprite WorldObject::GetGraphic()
{
  return _sprite;
}

注意

sf::Sprite GetGraphic()
{
  return _sprite;
}

,如果从程序中的某个位置调用此函数,C++ 编译器只会抱怨缺少 WorldObject::GetGraphic

The function GetGraphics that you define in WorldObject.cpp is not a member of class WorldObject. Use

sf::Sprite WorldObject::GetGraphic()
{
  return _sprite;
}

instead of

sf::Sprite GetGraphic()
{
  return _sprite;
}

Note that the C++ compiler only complains about the missing WorldObject::GetGraphic if this function is called from somewhere in your program.

韬韬不绝 2024-10-19 14:54:20

sf::Sprite GetGraphic() 不正确,它声明了一个全局 GetGraphic 函数。由于GetGraphic类WorldObject的一个函数,所以它应该是sf::Sprite WorldObject::GetGraphic()

sf::Sprite GetGraphic() is not correct, it is declaring a global GetGraphic function . Since GetGraphic is a function of the class WorldObject it should be sf::Sprite WorldObject::GetGraphic().

停顿的约定 2024-10-19 14:54:20

我没有做过太多 C++,但我认为您需要在 WorldObject.cpp 中使用 WorldObject::GetGraphic 而不是 GetGraphic

I haven't done much C++ but I think you need WorldObject::GetGraphic instead of GetGraphic in WorldObject.cpp?

雪落纷纷 2024-10-19 14:54:20

我相信你的意思是:

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

剑心龙吟 2024-10-19 14:54:20
// `GetGraphic()` is a member function of `WorldObject` class. So, you have two options to correct-
//Either define the functionality of `GetGraphic()` in the class definition itself. 

#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()  // Option 1
    {
         return _sprite;
    }
};
#endif

//When providing the member function definition, you need to declare that it is in class scope.  
// Option 2 => Just prototype in class header, but definition in .cpp
sf::Sprite WorldObject::GetGraphic() 
{  
    return _sprite;  
}
// `GetGraphic()` is a member function of `WorldObject` class. So, you have two options to correct-
//Either define the functionality of `GetGraphic()` in the class definition itself. 

#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()  // Option 1
    {
         return _sprite;
    }
};
#endif

//When providing the member function definition, you need to declare that it is in class scope.  
// Option 2 => Just prototype in class header, but definition in .cpp
sf::Sprite WorldObject::GetGraphic() 
{  
    return _sprite;  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文