C++ 向量大小为零
C++ Vector Size is Zero
我正在尝试创建一个函数来呈现 displayobject 对象向量中的所有内容(在另一个线程上)。我正在使用 SDL 线程。
这里是 displayobject.h:
class DisplayObject
{
protected:
int width;
int height;
int x;
int y;
SDL_Texture* texture;
SDL_Renderer* renderer;
public:
~DisplayObject();
int getX();
void setX(int x);
int getY();
void setY(int y);
int getWidth();
void setWidth(int width);
int getHeight();
void setHeight(int height);
SDL_Texture* getTexture();
SDL_Renderer* getRenderer();
};
在 graphics.h 我有这些变量:
std::vector<DisplayObject> imgArr;
SDL_Thread* renderThread;
static int renderLoop(void* vectorPointer);
此代码在图形构造函数中:
TextLabel textLabel(graphics->getRenderer(), 300, 80, "Hallo Welt", 50, Color(255, 0, 255), "Xenotron.ttf");
//TextLabel inherits from DisplayObject
imgArr.push_back(textLabel);
renderThread = SDL_CreateThread(Graphics::renderLoop, "renderLoop", &imgArr);
这是渲染循环函数:
int Graphics::renderLoop(void* param)
{
int counter = 0;
bool rendering = true;
std::vector<DisplayObject>* imgArr = (std::vector<DisplayObject>*)param;
while (rendering)
{
cout << imgArr->size() << endl;
counter++;
if (counter > 600)
{
rendering = false;
}
SDL_Delay(16);
}
return 0;
}
问题是它只在控制台中打印 0。为什么要这样做?它应该写 1 因为我将对象推入其中。
当你将一个TextLabel
插入std::vector<DisplayObject>
时,向量中存储的不是你原来的TextLabel
对象,而是一个DisplayObject
从TextLabel
。你想要做的是用 new
创建你的 TextLabel
,存储指向它们的指针,并在你不再需要它们时调用 delete
。
最好的解决方案是改用 boost::ptr_vector<DisplayObject>
- 当您从中删除对象时,它会自动调用 delete
。
http://www.boost.org/doc/libs/1_57_0/libs/ptr_container/doc/ptr_container.html
如果你不能使用Boost,但可以使用C++11,你可以使用std::vector<std::unique_ptr<DisplayObject>>
。
我正在尝试创建一个函数来呈现 displayobject 对象向量中的所有内容(在另一个线程上)。我正在使用 SDL 线程。
这里是 displayobject.h:
class DisplayObject
{
protected:
int width;
int height;
int x;
int y;
SDL_Texture* texture;
SDL_Renderer* renderer;
public:
~DisplayObject();
int getX();
void setX(int x);
int getY();
void setY(int y);
int getWidth();
void setWidth(int width);
int getHeight();
void setHeight(int height);
SDL_Texture* getTexture();
SDL_Renderer* getRenderer();
};
在 graphics.h 我有这些变量:
std::vector<DisplayObject> imgArr;
SDL_Thread* renderThread;
static int renderLoop(void* vectorPointer);
此代码在图形构造函数中:
TextLabel textLabel(graphics->getRenderer(), 300, 80, "Hallo Welt", 50, Color(255, 0, 255), "Xenotron.ttf");
//TextLabel inherits from DisplayObject
imgArr.push_back(textLabel);
renderThread = SDL_CreateThread(Graphics::renderLoop, "renderLoop", &imgArr);
这是渲染循环函数:
int Graphics::renderLoop(void* param)
{
int counter = 0;
bool rendering = true;
std::vector<DisplayObject>* imgArr = (std::vector<DisplayObject>*)param;
while (rendering)
{
cout << imgArr->size() << endl;
counter++;
if (counter > 600)
{
rendering = false;
}
SDL_Delay(16);
}
return 0;
}
问题是它只在控制台中打印 0。为什么要这样做?它应该写 1 因为我将对象推入其中。
当你将一个TextLabel
插入std::vector<DisplayObject>
时,向量中存储的不是你原来的TextLabel
对象,而是一个DisplayObject
从TextLabel
。你想要做的是用 new
创建你的 TextLabel
,存储指向它们的指针,并在你不再需要它们时调用 delete
。
最好的解决方案是改用 boost::ptr_vector<DisplayObject>
- 当您从中删除对象时,它会自动调用 delete
。
http://www.boost.org/doc/libs/1_57_0/libs/ptr_container/doc/ptr_container.html
如果你不能使用Boost,但可以使用C++11,你可以使用std::vector<std::unique_ptr<DisplayObject>>
。