不同范围内的C ++对象引用

时间:2018-12-12 01:01:02

标签: c++ object sdl

我有一个Rect类,它可以保存形状的宽度,高度,x和y值。该类可以使用参数中的值进行绘制并移动绘制的矩形。

     Rect::Rect(w, h, x, y, const std::string &image_path) : _w(w), _h(h), 
  _x(x), _y(y)
     {

     SDL_Surface *surface = IMG_Load(image_path.c_str());
     if (!surface) {
     std::cerr << "Failed to create surface.";

     }

    //create texture
    texture = SDL_CreateTextureFromSurface(Window::renderer, surface);
    if (!texture) {
    std::cerr << "Failed to create worker texture.";

     }

     SDL_FreeSurface(surface);

     }

     Rect::~Rect() 
     {
     SDL_DestroyTexture(texture);
     }

     Rect::draw()
     {
       //where the constructor parameters are parsed
       SDL_Rect rect= {_x, _y, _w, _h} ;

       //extra code about some SDL texture stuff and RenderCopy
     }

     Rect::moveX(int x){

     _x +=x;

     }

在我的Unit类中,包括Rect类,并创建我的单元,并在相同的函数中绘制它们。通过检查来自另一个已更改的类的另一个值,单元中还有另一个函数可移动rect。

  Unit::Unit()
  Unit::~Unit()

    void Unit::createUnit(int type, int x, int y){
      if (type == 0)
      {
      Rect unit1(unitImageWidth, unitImageSizeHeight, x, y, "res/unit1.png");

      }
      if (type == 1)
      {
      Rect unit2(unitImageWidth, unitImageSizeHeight, x, y, "res/unit2.png");

      }
    }

   void Unit::moveUnit(int x){
      if(selection == 0)
      {
      unit1.movex(x);
      }

      if (selection ==  1)
      {
      unit2.movex(x);
      }
    }

我的问题是:

Unit :: moveUnit()中时,如何引用在 Unit :: createUnit()中初始化的对象Rect“ unit1”和Rect“ unit2” >?

当我尝试编译时,它说unit1和unit2未定义。

2 个答案:

答案 0 :(得分:0)

您不能做自己想做的事。非static局部变量绑定到它们的scope。它们仅在内部可见,并且在程序退出由{}大括号界定的区域时被破坏。

最简单的解决方案是朝完全不同的方向发展。将Unit变量添加到private中以包含Rect,例如

Rect sprite;

然后替换

void createUnit(int type, int x, int y);

使用Unit构造函数

void Unit(int type, int x, int y);

并实现类似

的构造函数
void Unit::Unit(int type, int x, int y): sprite(unitImageWidth, 
                                                unitImageSizeHeight, 
                                                x, 
                                                y, 
                                                type == 0? "res/unit1.png": "res/unit2.png")
{

}

冒号:Member Initializer List开头,而疯狂的?:是一行,如果语句称为Ternary or Conditional Operator

注意:我不知道unitImageWidthunitImageSizeHeight是什么或它们来自何处。确保您已执行操作并确保它们可访问。

moveUnit变为

void Unit::moveUnit(int x)
{
    sprite.movex(x);
}

因为sprite知道它是什么,已经加载了什么图像,并且可以将Rect移到x(或movex所做的任何事情)。

使用您

Unit myUnit(0, 1024, 42);  // creates a unit of type 0 at coordinates 1024,42
myUnit.movex(88); // moves myUnit to 88,42 (I think)

答案 1 :(得分:0)

只需在类Unit中添加两个Rect成员,即可在不同的成员函数中使用它。
最好使用指针,如下所示:

class Unit
{
 public:
    Unit::Unit()
        : uint1(NULL), uint2(NULL){};
    Unit::~Unit()
    {
        if (uint1 != NULL) {
            delete unit1;
            uint1 = NULL;
        }
        if (uint2 != NULL) {
            delete unit2;
            uint2 = NULL;
        }
    };

    void Unit::createUnit(int type, int x, int y)
    {
        if (type == 0) {
            unit1 = new Rect(unitImageWidth, unitImageSizeHeight, x, y, "res/unit1.png");
        }
        if (type == 1) {
            unit2 = new Rect(unitImageWidth, unitImageSizeHeight, x, y, "res/unit2.png");
        }
    }

    void Unit::moveUnit(int x)
    {
        if (selection == 0) {
            unit1->movex(x);
        }

        if (selection == 1) {
            unit2->movex(x);
        }
    }

 private:
    Rect *unit1;
    Rect *unit2;
};