在C ++ Builder中动态分配图像数组 [英] Dynamic alocation an array of Images in C++ Builder
问题描述
我想制作一个BlackJack游戏。
I want to make a game of BlackJack.
我想制作图像矢量,并且当我按下按钮时会显示新图像(需要2或3张图片。)
I want to make a vector of images and when I press a button a new image shows up(need 2 or 3 images).
之后,我想在另一个按钮中将其全部删除。
After that I want to delete all of them in another button press.
我尝试在主类(Unit1.h)中声明向量:
I try to declare an vector in main class(Unit1.h):
class TForm1 : public TForm{
...
public: TImage* Images[];
在Form1(Unit1.cpp)中:
And in Form1(Unit1.cpp):
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// i=1 is global
Images[i] =new TImage(this);
Images[i]->Parent=this;
Images[i]->Picture->Bitmap->...//here i want to use it
i++;
}
在这里我要删除它们:
void __fastcall TForm1::Button3Click(TObject *Sender)
{
for(int k=1;k<=i;i++) {
Images[k]->~TImage(); //delete Images[k];
}
}
推荐答案
您没有声明向量。 std :: vector
是C ++中的实际类类型。您实际声明的是一个未知大小的静态数组。你不能这样做。您必须在编译时为该大小指定一个常量,例如:
You did not declare a vector. std::vector
is an actual class type in C++. What you actually declared is a static array of unknown size. You cannot do that. You must specify a constant for the size at compile-time, eg:
class TForm1 : public TForm
{
...
public:
TImage* Images[3];
};
如果您不知道编译时的映像数量,则必须声明指针指向在运行时动态分配的数组:
If you do not know the number of images at compile-time, you must declare a pointer to an array that is dynamically allocated at runtime instead:
class TForm1 : public TForm
{
...
public:
TImage** Images;
int TotalNumberOfImages;
int NumberOfImages;
__fastcall TForm1(TComponent *Owner);
__fastcall ~TForm1();
};
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
TotalNumberOfImages = ...;
Images = new TImage*[TotalNumberOfImages];
NumberOfImages = 0;
}
__fastcall TForm1::~TForm1()
{
delete[] Images;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (NumberOfImages < TotalNumberOfImage)
{
TImage *img = new TImage(this);
img->Parent = this;
img->Picture->...
...
Images[NumberOfImages++] = img;
}
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
for(int k = 0; k < NumberOfImages; ++k)
delete Images[k];
NumberOfImages = 0;
}
如果您真的想使用 std ::向量
,应该更像这样:
If you really want to use a std::vector
, it should be more like this instead:
#include <vector>
class TForm1 : public TForm
{
...
public:
std::vector<TImage*> Images;
};
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TImage *img = new TImage(this);
img->Parent = this;
img->Picture->...
...
Images.push_back(img);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
for(int k = 0; k < Images.size(); ++k)
delete Images[k];
/*
or:
for(std::vector<TImage*>::iterator iter = Images.begin(), end = Images.end(); iter != end; ++iter)
delete *iter;
*/
Images.clear();
}
或者,如果您使用的是Embarcadero基于Clang的C ++ 11之一编译器:
Or, if you are using one of Embarcadero's Clang-based C++11 compilers:
#include <vector>
#include <memory>
class TForm1 : public TForm
{
...
public:
std::vector<std::unique_ptr<TImage*>> Images;
};
void __fastcall TForm1::Button1Click(TObject *Sender)
{
std::unique_ptr<TImage> img(new TImage(this));
img->Parent = this;
img->Picture->...
...
Images.push_back(img);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Images.clear();
}
这篇关于在C ++ Builder中动态分配图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!