简体   繁体   English

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

[英]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. 多亏了我在乐施会书店里找到的一本小书和一本大书,我一直在玩C,C ++和Allegro。 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];' 根据我从调试输出中收集到的信息,第一个错误是关于“粒子Particle [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. 行,并且消息听起来好像在'粒子'的末尾加上'[max]'是错误的,但是到现在为止,它可以正常工作并且编译没有问题。 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++. 在C ++中用const表示。 In C, you'd use a #define . 在C语言中,您将使用#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++. 标准C ++中不允许使用VLA。

Use this: 用这个:

const int max = 50;

Because array size must be a constant expression. 因为数组大小必须是一个常量表达式。 Without const keyword, max is not a constant expression. 如果没有const关键字,则max不是常量表达式。

Change to const int max = 50; 更改为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) (请注意,并非所有 const变量都是编译时间常数)

Also, rename the file to *.cpp , so GCC will understand the C++ constructs. 另外,将文件重命名为 *.cpp ,这样GCC将理解C ++构造。 It seems to be compiling as straight up C language code. 它似乎被编译为直接的 C语言代码。
Joachim Pileborg observed you have both Vector2d and Vector2D . 乔阿希姆·皮勒伯格(Joachim Pileborg)观察到您同时拥有Vector2dVector2D

I doubt this code would ever compile. 我怀疑这段代码是否会编译。

  • Vector2D is not a correct type. Vector2D不是正确的类型。 struct Vector2D is. struct Vector2D是。 (Many errors arise because of this). (因此会出现许多错误)。 You can use typedef struct Vector2D Vector2D to get the expected behaviour. 您可以使用typedef struct Vector2D Vector2D获得预期的行为。 Not sure what's going on there with the Vector2d (lowercase d ). 不知道Vector2d (小写d )发生了什么。
  • You cannot dynamically define the space of a global table variable. 您不能动态定义全局表变量的空间。 If you do particles[50] it will work. 如果您做particles[50] ,它将起作用。 It will also work if you #define max 50 or enum { max = 50 }; 如果您#define max 50enum { max = 50 };它也将起作用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() . 否则,您可能会选择使用malloc()为粒子动态分配空间-您必须在main()执行此操作。 The problem here is that int max = 0; 这里的问题是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. 如果您使用的是C,则定义const将无济于事,因为它意味着只读而不是常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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