2011-11-16 62 views
0

我一直在玩C,C++和Allegro感謝一本小書和一本我在Oxfam書店發現的大書。 我理解它是相當不錯的時刻,但我已經打了一堵牆......每當我編譯我得到這些錯誤:C++(貌似)隨機編譯器錯誤

[email protected]:~/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]'是錯誤的,但是直到現在,這個工作正常並且編譯沒有問題。這可能只是一個錯字或誤解,但我真的無法弄清楚。

正如你可以看到它是在一個粒子系統的要求,並在砥礪任何提示(就是一個字?)我的代碼是極大的讚賞:)

感謝。

+0

嘗試宣告最大爲'const int的最大值= 50' –

+4

'Vector2D'或'Vector2d' ......選擇一個...;) –

+1

這個問題被標記C++和C,但問題是問關於C++而文件名稱表示C. C++和C是兩種不同的語言。您還將該文件編譯爲C++。挑一個...... – bames53

回答

7

要使變量能夠用作數組大小,它需要是一個常量表達式。這在C++中用const表示。在C中,您將使用#define

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

現在就可以了,非常感謝! :D – archiboldian

+1

在這兩種語言中,你也可以使用'enum'常量。 –

+0

@Jens:我知道我忘了一些東西!謝謝。 – Xeo

2

錯誤解釋該問題:

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

的修復:

const int max = 50; 

現在結合的陣列是一個整常數。

+0

感謝您解釋錯誤和問題之間的關係。 – archiboldian

1

在標準C++中不允許使用VLA。

使用此:

const int max = 50; 

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

0

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

另外,文件重命名爲 *.cpp,所以GCC就明白++構建℃。它似乎是直接編譯爲 C語言代碼。
Joachim Pileborg觀察到你有Vector2dVector2D

0

我懷疑這個代碼會永遠編譯。

  • Vector2D是不正確的類型。 struct Vector2D是。 (由於這個原因產生了許多錯誤)。您可以使用typedef struct Vector2D Vector2D來獲得預期的行爲。不知道Vector2d(小寫d)是怎麼回事。
  • 您不能動態定義全局表變量的空間。如果你這樣做particles[50]它會起作用。它也可以工作,如果你#define max 50enum { max = 50 };。在那裏,爲了這個目的,枚舉變體可能是你最好的選擇。否則,您可以選擇使用malloc()爲粒子動態分配空間 - 您必須在main()中執行此操作。這裏的問題是int max = 0;不是恆定的。如果使用C,則定義const將無濟於事,因爲它表示只讀而非常量。
+0

關於'Vector2D'和'struct Vector2D'的第一點不適用於C++。你的第二點也是C特定的。儘管問題中使用的文件名錶示語言是C,但問題標題是C++,文件正在編譯爲C++。 – bames53

+0

嘿,那麼對不起。我查看了代碼並沒有看到任何C++特定的東西,所以我簡單地認爲它是C.但我感興趣 - 如果我在C++中得到了正確的結果,那麼結構會自動爲您定製typedefd?我沒有做很多(或任何)C++。 :) – Matej

+0

它不是那麼神奇地typedef'd,因爲它是C++只允許一個沒有struct關鍵字引用結構。也許這是一個迂腐的區分,但有一點不同,即使在一個struct的定義中,你也可以使用struct的undened名稱,而使用typedefs你不能這麼做:'C++:struct Foo {Foo * f; };' – bames53