簡體   English   中英

C ++(貌似)隨機編譯器錯誤

[英]C++ (seemingly) Random Compiler Errors

多虧了我在樂施會書店里找到的一本小書和一本大書,我一直在玩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();

根據我從調試輸出中收集到的信息,第一個錯誤是關於“粒子Particle [max];”的問題。 行,並且消息聽起來好像在'粒子'的末尾加上'[max]'是錯誤的,但是到現在為止,它可以正常工作並且編譯沒有問題。 這可能只是拼寫錯誤或誤解之類的東西,但我真的不知道。

如您所見,這是對粒子系統的嘗試,並且有任何改進的提示(這是一個詞嗎?),我的代碼非常受人們歡迎:)

謝謝。

為了使變量能夠用作數組大小,它必須是一個常量表達式。 在C ++中用const表示。 在C語言中,您將使用#define

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

該錯誤說明了問題:

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

解決方法:

const int max = 50;

現在,數組綁定是一個整數常量。

標准C ++中不允許使用VLA。

用這個:

const int max = 50;

因為數組大小必須是一個常量表達式。 如果沒有const關鍵字,則max不是常量表達式。

更改為const int max = 50; 這是一個編譯時間常數,因此可以用來初始化數組。 (請注意,並非所有 const變量都是編譯時間常數)

另外,將文件重命名為 *.cpp ,這樣GCC將理解C ++構造。 它似乎被編譯為直接的 C語言代碼。
喬阿希姆·皮勒伯格(Joachim Pileborg)觀察到您同時擁有Vector2dVector2D

我懷疑這段代碼是否會編譯。

  • Vector2D不是正確的類型。 struct Vector2D是。 (因此會出現許多錯誤)。 您可以使用typedef struct Vector2D Vector2D獲得預期的行為。 不知道Vector2d (小寫d )發生了什么。
  • 您不能動態定義全局表變量的空間。 如果您做particles[50] ,它將起作用。 如果您#define max 50enum { max = 50 };它也將起作用enum { max = 50 }; 為此,枚舉變量可能是您最好的選擇。 否則,您可能會選擇使用malloc()為粒子動態分配空間-您必須在main()執行此操作。 這里的問題是int max = 0; 不是恆定的。 如果您使用的是C,則定義const將無濟於事,因為它意味着只讀而不是常量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM