電子書籍の厳選無料作品が豊富!

C++BuilderXE2starterです。
ビットマップ(bmp)やTImage を配列で定義し、関数の引数として渡したいのですが
よくわからないまま、次のようにすると、色んな不正な使い方らしきエラーとなります。
正しい使い方を教えてください。
なお配列でないbmpを渡すことはできます。ImageはIDEで定義するのではなく、プログラムから生成させる積もりです。
最終的にやりたいスタイル。
void __fastcall TForm1::onclick(TObject *Sender)
{
intn;
Graphics::TBitmap *bmp[10];
TImage::Image *Imagenum[10];

for(n=0;n<10;n++){
bmp[n]=new Graphics::TBitmap();
Imagenum[n]=new TImage::TImage;
// bmp,Imagenum のサイズ位置の設定
Imagenum[n]->Top= 値;
Imagenum[n]->Left= 値;
}

sub(bmp,Imagenum);

Free(bmp); // for でまわす要あり?
Free(Image);

}
//---------------------------------------------------------------------------
voidTForm1::sub2(Graphics::TBitmap *bmp[], TImage *Imagenum[])
{
Image[0]->Canvas->Draw(0,0,bmp[0]);
Image[1]->Canvas->Draw(0,0,bmp[1]);
}

unit.h
class TForm1 : public TForm
{
__published:// IDE で管理されるコンポーネント
TButton *Button1;
void __fastcall onclick(TObject *Sender);
private:// ユーザー宣言
voidTForm1::sub(Graphics::TBitmap **bmp, TImage **Image);

public:// ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};

A 回答 (3件)

おそらく、やりたいことは次のようなコートだと思います。



void sub2(TBitmap* bmp[10], TImage* image[10])
{
for (int i = 0; i < 10; ++i)
image[i]->Canvas->Draw(0, 0, bmp[i]);
}
void __fastcall TForm1::FormClick(TObject *Sender)
{
TBitmap* bmp[10];
TImage* image[10];
for (int i = 0; i < 10; ++i)
{
bmp[i] = new TBitmap();
bmp[i]->LoadFromFile(IntToStr(i) + ".bmp");

image[i] = new TImage(this);
image[i]->Parent = this;
image[i]->Left = i * 32;
image[i]->Top = 0;
image[i]->Height = 32;
image[i]->Width = 32;
}

sub2(bmp, image);

for (int i = 0; i < 10; ++i)
delete bmp[i];
}

せっかくC++を使うなら配列よりもstd::arrayやstd::vectorなどを使った方がいいと思います。

画像ファイルを表示するだけなら次のようにかけます。

void __fastcall TForm1::Button1Click(TObject *Sender)
{
for (int i = 0; i< 10; ++i)
{
TImage* image = new TImage(this);
image->Parent = this;
image->Left = i * 32;
image->Top = 0;
image->Height = 32;
image->Width = 32;
image->Picture->LoadFromFile(IntToStr(i) + ".bmp");
}
}

この回答への補足

あと、忘れました。
Delete Image[n]がなくても良い点が疑問です。

補足日時:2012/12/26 22:00
    • good
    • 0
この回答へのお礼

T.yamamoto様(お世話になっている方のイニシアルに覚えがありますが・・)ご返事ありがとうございます。
書き方はわかりました。意味も大体わかりました。
記法のバラエティとして以下を確認しました。
・image[i] = new TImage(this);→image[i] = new TImage(Form1);
前者の方が一般的なのですね。
・void sub2(TBitmap* bmp[10], TImage* image[10])  →
    void sub2(TBitmap** bmp, TImage** image)
  同じならば、前者の方が実体をよくあらわしているのでベターですね。

理屈は体で(指で?)覚えていないので、すぐ忘れてしまいます。できるだけ忘れない方法でメモしておきます。

他 std::array はスマートポインタですね。触手のでる良い点もありますがどうも新しいことにはなかなかなじめません。
Fotranの添え字チェックにもあたりますね。

image[n]の使い方のご提示で、規則的配置とも限らないのですが、参考になります。

お礼日時:2012/12/26 21:57

C++Builder は全然知らんのだけど, TImage::Image とか TImage::TImage って存在するの?



4つ目 (29行目) のエラーはこれらとは全然関係なくって, 単に「配列の渡し方を理解しているかどうか」だけの問題.
    • good
    • 0
この回答へのお礼

引き続き C++Builder をご存知の方のご返事をお待ちします。

お礼日時:2012/12/26 09:05

・unit.h で TForm1::sub2 が宣言されていないのはなぜ?


・TForm1::sub2 の中にある Image ってなに?

Free は for で回さないとだめだね.
    • good
    • 0
この回答へのお礼

ご返事ありがとうございます。
投稿時にミスがあったようです。現在のコードとそのエラーを開示します。
未定義のシンボルImageがよくわからないです。

[BCC32 エラー] Unit1.cpp(21): E2451 未定義のシンボル Image    *1
[BCC32 エラー] Unit1.cpp(21): E2451 未定義のシンボル Imagenum*2
[BCC32 エラー] Unit1.cpp(26): E2303 型名が必要*3
他に下記エラー等もあるが、上記と関連していると思います
[BCC32 エラー] Unit1.cpp(29): E2034 'TBitmap *' 型は 'TBitmap * *' 型に変換できない

unit.h 抜粋
private:// ユーザー宣言
voidTForm1::sub2(Graphics::TBitmap **bmp, TImage **Image);


unit1.cpp
void __fastcall TForm1::onclick(TObject *Sender)
{
intn;
Graphics::TBitmap *bmp[10];
TImage::Image *Imagenum[10];    // *1,*2
for(n=0;n<10;n++){
bmp[n]=new Graphics::TBitmap();
Imagenum[n]=new TImage::TImage; //*3 (*1,*2正しくないから?)
}

sub2(*bmp,*Imagenum);

for(n=0;n<10;n++){
Free(bmp[n]);
Free(Imagenum[n]);
}
}
//---------------------------------------------------------------------------
voidTForm1::sub2(Graphics::TBitmap *bmp[], TImage *Imagenum[])
{
Imagenum[0]->Canvas->Draw(0,0,bmp[0]);
Imagenum[1]->Canvas->Draw(0,0,bmp[1]);
}

お礼日時:2012/12/25 12:46

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!