0

Oxfam の書店で見つけた小さな本と大きな本のおかげで、私は C、C++、Allegro で遊んでいます。現時点ではよく理解していますが、壁にぶつかりました...コンパイルするたびに、次のエラーが発生します。

archiboldian@archiboldian:~/Documents/C++ Projects/particles$ g++ particles.c -lalleg -lnoise -o particles
particles.c:19: error: array bound is not an integer constant before ‘]’ token
particles.c:20: error: ‘Vector2D’ does not name a type
particles.c:21: error: ‘Vector2D’ does not name a type
particles.c: In function ‘int main()’:
particles.c:26: error: ‘nPos’ was not declared in this scope
particles.c:28: error: ‘nVel’ was not declared in this scope
particles.c:29: error: ‘nvel’ was not declared in this scope
particles.c:31: error: ‘addParticle’ was not declared in this scope
particles.c: At global scope:
particles.c:47: error: ‘Vector2D’ has not been declared
particles.c:47: error: ‘Color’ has not been declared
particles.c: In function ‘void addParticle(int, int, Vector2d, int, int, int)’:
particles.c:50: error: ‘particles’ was not declared in this scope

そして、これは私のコードです...

#include "allegro.h"

struct Vector2d{
    double x;
    double y;
};

struct Particle {
    Vector2d Pos;
    Vector2d Vel;
    int age;
    int LifeSpan;
    int colour;
    int size;
};

int max = 50;
int pcount = 0;
Particle particles[max];

int main(void) {

    Vector2D nPos;
    Vector2D nVel;

    nPos.x = 320;
    nPos.y = 240;
    nVel.x = 2;
    nvel.y = 0;

    addParticle(10, nPos, nVel, 20, makecol(255,255,255), 2);

    allegro_init();
    install_keyboard();

    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

    while(!key[KEY_ESC]) {
        for(int i=0;i<pcount;i++){

        }
    }

    allegro_exit();
}

void addParticle(int addp, Vector2D Pos, Vector2d Vel, int LifeSpan, Color colour, int size) {
    for(int i=0;i<addp;i++){
        pcount++;
        particles[pcount].Pos = Pos;
        particles[pcount].Vel = Vel;
        particles[pcount].LifeSpan = LifeSpan;
        particles[pcount].colour = colour;
        particles[pcount].size = size;
    }
}

END_OF_MAIN();

デバッグ出力から収集したものから、最初のエラーは「粒子粒子[最大];」の問題について話している. 行とメッセージは、「粒子」の最後にこの「[max]」を付けるのは間違っているように聞こえますが、それは正常に機能し、今まで問題なくコンパイルされていました. 単なるタイプミスか誤解か何かかもしれませんが、私には本当にわかりません。

あなたが見ることができるように、それは粒子システムでの試みであり、改善に関するヒントです(それは言葉ですか?)私のコードは大歓迎です:)

ありがとう。

4

5 に答える 5

7

変数を配列サイズとして使用できるようにするには、定数式である必要があります。constこれは、C++ ではwith で示されます。C では、a を使用し#defineます。

// C++
const int MAX = 50;
/* C */
#define MAX 50
/* both C & C++ */
enum { MAX = 50 };
Particle particles[MAX];
于 2011-11-16T17:43:29.740 に答える
2

エラーは問題を説明しています:

particles.c:19: error: array bound is not an integer constant before ‘]’ token

修正:

const int max = 50;

現在、バインドされている配列は整数定数です。

于 2011-11-16T17:43:37.823 に答える
1

VLA は、標準 C++ では許可されていません。

これを使って:

const int max = 50;

配列サイズは定数式でなければならないためです。constキーワードがなければmax、定数式ではありません。

于 2011-11-16T17:44:11.030 に答える
0

which をコンパイル時の定数に変更しconst int max = 50;て、配列の初期化に使用できるようにします。(すべての const変数がコンパイル時の定数ではないことに注意してください)

また、ファイルの名前を に変更して*.cpp、GCC が C++ 構造を理解できるようにします。Cまっすぐな言語コードとしてコンパイルされているようです。
Joachim Pileborg は、あなたが と の両方を持っていることを観察Vector2dVector2Dました。

于 2011-11-16T17:44:45.997 に答える
0

このコードがコンパイルされるとは思えません。

  • Vector2Dは正しいタイプではありません。struct Vector2Dは。(このため、多くのエラーが発生します)。typedef struct Vector2D Vector2D期待される動作を得るために使用できます。Vector2d(小文字)で何が起こっているのかわかりませんd
  • グローバル テーブル変数のスペースを動的に定義することはできません。やればうまくparticles[50]いく。#define max 50またはの場合にも機能しますenum { max = 50 };。そのような目的では、enumバリアントがおそらく最善の策です。それ以外の場合は、 を使用してパーティクルにスペースを動的に割り当てることを選択できますmalloc()— でこれを行う必要がありますmain()。ここでの問題は、それint max = 0;が一定ではないということです。const定数ではなく読み取り専用を意味するため、C を使用している場合は定義しても役に立ちません。
于 2011-11-16T17:52:09.177 に答える