C++编译器如何用矢量构建动态对象
C++ Builder how to build dynamic Objects with vector?
我正在C++Builder中开发IOS应用程序。我的问题是如何在for循环中构建TImage。我公开声明了矢量:
#include <vector>
std::vector<TImage*> Image(c); // public declaration
void __fastcall TForm1::Button2Click(TObject *Sender)
{
for (int i = 0; i < c ; i++){
c = 5 // c should be send to te array
Image[i] = new TImage(this); // I tried it this way but when i click the button i get an acess violence error
Image[i]->Parent = BoardItem ;
Image[i]->Height = 20 ;
Image[i]->Width = 20 ;
}
}
那么,如何使用向量在for循环中创建图像呢?
在C++Builder中动态定位一组图像我查找了这个问题的最后一个答案,但没有描述如何在循环中进行。
// c should be send to te array
如果更改变量c
的值,它不会更改数组的大小。
您必须在Image
向量上调用resize()
才能更改大小,但这并不合适。最好写一些类似的东西
void TForm1::ClearImage() {
for (int i = 0; i < Image.size(); ++i) {
delete Image[i];
}
Image.clear();
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ClearImage();
c=5;
for (int i = 0; i < c ; i++){
Image.push_back(new TImage(this));
Image.back()->Parent = BoardItem ;
Image.back()->Height = 20 ;
Image.back()->Width = 20 ;
}
}
相关文章:
- 如何在Windows上链接动态构建的cmake文件
- 如何使用cmake设置OpenCV以启用动态和静态构建
- 动态构建模板参数包
- 如何在C 构建器中动态声明C#DLL
- 动态变量名称构建,连接
- C/C 可以将所有动态库构建到一个bin文件中
- 构建增压动态可链接会产生链接错误
- 如何为动态类型语言构建编译器
- 如何从一组已定义的描述符动态构建新的 protobuf
- 如何动态构建和链接带有隐藏内联的 boost::regex
- 链接器错误构建 fibonnacci 示例与 boost::coroutine2 与 Boost 1.60 使用动态链接
- 构建动态分配的类对象数组
- 如何在 Mac OS X 上构建和使用动态库
- C++编译器如何用矢量构建动态对象
- 用char*构建动态结构的更好方法
- Makefile:强制项目在构建时使用动态或静态库
- GCC:指定要构建的静态/动态库
- 用于构建在C++(Linux)中开发的动态库的TemplateMakefile
- Gnat gprbuild:如何构建动态DLL并与静态c++库链接
- 如何在c++中构建动态数组并将其返回到c# /. net