c ++中对静态变量的未定义引用

时间:2015-05-06 19:47:12

标签: c++ static rect

我试图在我的一个班级中使用静态变量。我之前使用过它们,没有问题。现在我收到了这个错误:

  

/tmp/ccg26aZi.o:在函数' main':main.cpp :(。text + 0x7482):   未定义的引用`Rect :: rect_change' collect2:错误:ld   返回1退出状态make:*** [exe]错误1

任何建议?谢谢!

更新:

我被告知检查我的make文件以确保rect.cpp被包含在exe的制作中。我切换了文件夹,使其处于正确的位置。但现在我得到了一整套新的错误,我之前没有得到:

我检查了我的make文件并确保了rect.cpp,并将其移动到另一个运行* .cpp的文件夹中。但是现在我得到了一系列全新的错误:

  
    

includes / Rect.h:16:17:错误:'SDL_Rect'没有命名类型Rect(const SDL_Rect& r)

         

includes / Rect.h:16:28:错误:ISO C ++禁止声明'r'没有类型[-fpermissive] Rect(const SDL_Rect& r)

  
     

includes / Rect.h:19:2:错误:'SDL_Rect'没有命名类型
  SDL_Rect getSDL_Rect()^ includes / Rect.h:在构造函数中   'Rect :: Rect(const int&)':includes / Rect.h:17:9:error:request for   'r'中的成员'x',它是非类型'const int':x(r.x),   y(r.y),w(r.w),h(r.h)

     

includes / Rect.h:17:17:错误:请求'r'中的成员'y',即   非类型'const int':x(r.x),y(r.y),w(r.w),h(r.h)

     

includes / Rect.h:17:25:错误:请求'r'中的成员'w',即   非类型'const int':x(r.x),y(r.y),w(r.w),h(r.h)

     

includes / Rect.h:17:33:错误:请求'r'中的成员'h',即   非类型'const int':x(r.x),y(r.y),w(r.w),h(r.h)

这是我的make文件的内容。 rect.cpp位于src文件夹中。

exe:    main.cpp
    g++ *.cpp src/*.cpp src/*.c `sdl-config --cflags --libs` -lSDL_image -lSDL_mixer -lSDL_ttf -Iincludes 

run:
    ./a.out

r:
    ./a.out

clean:
    rm a.out

c:
    rm a.out
// header
#ifndef RECT_H
#define RECT_H

#include <iostream>

class Rect
{
public:
    Rect(int x0 = 0, int y0 = 0, int w0 = 0, int h0 = 0)
        : x(x0), y(y0), w(w0), h(h0)
    {}
    Rect( const SDL_Rect & r)
        : x(r.x), y(r.y), w(r.w), h(r.h)
    {}
    SDL_Rect getSDL_Rect()
    {
        SDL_Rect r = {x, y, w, h};
        return r;
    }
    int x, y, w, h;
    static int rect_change;
    static int rect_change_max;
};

inline std::ostream & operator<<(std::ostream & cout, const Rect & r)
{
    cout << "(" << r.x << "," << r.y << "," << r.w << "," << r.h << ")";
    return cout;
}

#endif
rect.cpp    
#include "Rect.h"

int Rect::rect_change = 0;
int Rect::rect_change_max = 0;
   // main.cpp example
   #include "Rect.h"

   int main()
   {
       Rect rect;
       rect.rect_change = 5;

       return 0;
   }

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,感谢RichieHindle的回答。

我需要#include&#34; SDL_gfxPrimitives.h&#34;让它工作。有趣的是,因为我已经在这个项目上工作了一段时间,并且编译器根本没有抱怨,直到我试图添加一个静态成员。

再次感谢!

相关问题