我正在尝试在我的一个类中使用静态变量 . 我以前用过它们,没问题 . 现在我收到这个错误:
/tmp/ccg26aZi.o:在函数'main'中:main.cpp :( . text 0x7482):未定义引用`Rect :: rect_change'colle2:错误: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)包含/ Rect.h:16:28:错误:ISO C禁止声明'r'没有type [-fpermissive] Rect(const SDL_Rect&r)包含/ Rect.h:19:2:错误:'SDL_Rect'没有命名类型SDL_Rect getSDL_Rect()^ includes / Rect.h:在构造函数'Rect :: Rect中(const int&)':includes / Rect.h:17:9:error:请求'r'中的成员'x',它是非类型'const int':x(rx),y(ry) ,w(rw),h(rh)包含/ Rect.h:17:17:错误:请求'r'中的成员'y',它是非类型'const int':x(rx), y(ry),w(rw),h(rh)包含/ Rect.h:17:25:错误:请求'r'中的成员'w',它是非类型'const int':x (rx),y(ry),w(rw),h(rh)包含/ Rect.h:17:33:错误:请求'r'中的成员'h',它是非类型的'const' int':x(rx),y(ry),w(rw),h(rh)
这是我的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 回答
感谢所有的帮助,谢谢RichieHindle的答案 .
我需要#include“SDL_gfxPrimitives.h”让它工作 . 有趣的是,因为我已经在这个项目上工作了一段时间,编译器根本没有抱怨,直到我试图添加一个静态成员 .
再次感谢!