【问题标题】:How is this private variable "not declared in this scope"?这个私有变量如何“未在此范围内声明”?
【发布时间】:2011-06-12 05:50:59
【问题描述】:

我目前正在尝试更多地了解 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

我在这段代码中遗漏了什么?迄今为止,试图理解设置继承层次结构的正确方法一直是游戏开发中的最大问题,但我知道这主要是因为我更习惯于使用 Java 的继承模型而不是 C++ 的多重继承模型。继承模型。

【问题讨论】:

    标签: c++ oop scope sfml declare


    【解决方案1】:

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

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

    而不是

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          我相信你的意思是:

          sf::Sprite WorldObject::GetGraphic()

          不是

          sf::Sprite GetGraphic()

          在 WorldObject.cpp 中

          【讨论】:

            【解决方案5】:
            // `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;  
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-09-18
              • 2017-02-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-08-19
              • 1970-01-01
              相关资源
              最近更新 更多