C ++(看似)随机编译器错误

时间:2011-11-16 17:40:52

标签: c++ c compiler-construction compiler-errors allegro

由于我在乐施会书店找到的一本小书和一本更大的书,我一直在玩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];'线条和消息听起来像是'粒子'末尾的'[max]'是错误的,但这样做工作正常并且直到现在才编译没有问题。这可能只是一个错字或误解或其他什么,但我真的无法理解。

正如你所看到的那样是对粒子系统的尝试以及任何有关改善的提示(是一个单词?)我的代码非常感谢:)

感谢。

5 个答案:

答案 0 :(得分:7)

要使变量能够用作数组大小,它必须是常量表达式。这在C ++中用const表示。在C中,您使用的是#define

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

答案 1 :(得分:2)

错误解释了问题:

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

修复:

const int max = 50;

现在数组绑定是一个整数常量。

答案 2 :(得分:1)

标准C ++中不允许使用VLA。

使用此:

const int max = 50;

因为数组大小必须是常量表达式。如果没有const关键字,max不是常量表达式。

答案 3 :(得分:0)

更改为const int max = 50;这是一个编译时常量,因此可用于初始化数组。 (注意,所有 const变量都不是编译时常量)

此外,将文件重命名为*.cpp,因此GCC将理解C ++结构。它似乎是直接编译C语言代码。
Joachim Pileborg发现你有Vector2dVector2D

答案 4 :(得分:0)

我怀疑这段代码是否会编译。

  • Vector2D不是正确的类型。 struct Vector2D是。 (因此出现了许多错误)。您可以使用typedef struct Vector2D Vector2D来获取预期的行为。不确定Vector2d(小写d)。
  • 会发生什么
  • 您无法动态定义全局表变量的空间。如果你做particles[50]它会起作用。如果您#define max 50enum { max = 50 };,它也会有效。在这种情况下,enum变体可能​​是你最好的选择。否则,您可以选择使用malloc()为粒子动态分配空间 - 您必须在main()中执行此操作。这里的问题是int max = 0;不是常数。如果使用C语言,定义它const将无济于事,因为它意味着只读而不是常量。
相关问题