简体   繁体   中英

C++ (seemingly) Random Compiler Errors

I've been playing around with C, C++ and Allegro thanks to a little book and a bigger book I found in an Oxfam book shop. I'm understanding it quite well at the moment but I've hit a wall... Whenever I compile I get these errors:

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

And this is my code...

#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();

From what I gather from the debug output the first error is talking about a problem with the 'Particle particles[max];' line and the message sounds like it's wrong to have this '[max]' at the end of 'particles' but that was working fine and compiling without problems until now. It's probably just a typo or a misunderstanding or something but I really can't figure it out.

As you can see it's an attempt at a particle system and any hints on bettering (is that a word?) my code are greatly appreciated :)

Thanks.

For a variable to be able to be used as an array size, it needs to be a constant expression. This is denoted with const in C++. In C, you'd use a #define .

// C++
const int MAX = 50;
/* C */
#define MAX 50
/* both C & C++ */
enum { MAX = 50 };
Particle particles[MAX];

The error explains the problem:

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

The fix:

const int max = 50;

Now the array bound is an integer constant.

VLA is not allowed in the Standard C++.

Use this:

const int max = 50;

Because array size must be a constant expression. Without const keyword, max is not a constant expression.

Change to const int max = 50; which is a compile time constant, so it can be used to initialize the array. (Note that not all const variables are compile time constants)

Also, rename the file to *.cpp , so GCC will understand the C++ constructs. It seems to be compiling as straight up C language code.
Joachim Pileborg observed you have both Vector2d and Vector2D .

I doubt this code would ever compile.

  • Vector2D is not a correct type. struct Vector2D is. (Many errors arise because of this). You can use typedef struct Vector2D Vector2D to get the expected behaviour. Not sure what's going on there with the Vector2d (lowercase d ).
  • You cannot dynamically define the space of a global table variable. If you do particles[50] it will work. It will also work if you #define max 50 or enum { max = 50 }; . Where, for such purposes, the enum variant is probably your best bet. Otherwise, you may opt to dynamically allocate space for particles using malloc() — you would have to do this in main() . The problem here is that int max = 0; is not constant. Defining it const will not help if you are using C, as it means read-only rather than constant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM