Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Multiple declaration of a variable? Compile error I can't figure out

Credits go to Christopher Webb for helping out!
This thread is locked; no one can reply to it. rss feed Print
Multiple declaration of a variable? Compile error I can't figure out
Durnus
Member #7,997
November 2006
avatar

multiple definition of `particle'
first defined here
ld returned 1 exit status
[Build Error] ["Wizard] Error 1

Those are the compile errors of my program. It doesn't tell me where the declarations, and when I do a "find" for the declaration, I only find one declaration, and that is in the header file. :-/

Don't complain about the code, the main file was written by my little file.

character.h

1//The Character header file.\\
2 
3#ifndef CHARACTER_H
4#define CHARACTER_H
5 
6int particle[640][480]; //Collision array
7 
8class character
9{
10 public:
11 
12void jump();
13void left();
14void right();
15void duck();
16 
17void update_pos();
18 
19int direction;
20
21int x; //X position
22int y; //Y position
23int xvel; //X velocity
24int yvel; //Y velocity
25 
26};
27 
28#endif

character.cpp:

1#include "character.h"
2#include <stdlib.h>
3 
4 
5void character::update_pos()
6{
7 if(particle[x][y+1] == 0)
8 {
9 yvel++;
10 }
11 int i = yvel;
12 while(i != 0)
13 {
14 if(particle[x][y+(i/(abs(i)))] == 0)
15 {
16 y = y + (i/(abs(i)));
17 }
18 else
19 {
20 i = 0;
21 }
22 if(i > 0) i--;
23 if(i < 0) i++;
24 }
25}
26 
27void character::left()
28{
29 if(xvel > -5)
30 {
31 xvel--;
32 }
33 direction = 2;
34}
35void character::right()
36{
37 if(xvel < 5)
38 {
39 xvel++;
40 }
41 direction = 1;
42}
43void character::jump()
44{
45 if(particle[x][y+1] == 0)
46 {
47 yvel = -10;
48 }
49}
50void character::duck()
51{
52
53}

main.cpp: (Written by little brother)

1#include <allegro.h>
2#include <cstdlib>
3#include <ctime>
4#include <string>
5 
6#include "character.h"
7 
8using namespace std;
9 
10int screenHeight = 480;
11int screenWidth = 640;
12BITMAP* buffer;
13 
14BITMAP* wizbit;
15BITMAP* wizbit2;
16BITMAP* slime;
17int action = 1;
18 
19 
20void draw()
21{
22 for(int x=0;x <screenWidth;++x)
23 {
24 for(int y=0; y < screenHeight; y++)
25 {
26 if (particle[x][y] == 1 )
27 {
28 putpixel(buffer, x,y, makecol(255,217,128));
29 }
30 if (particle[x][y] == 2 )
31 {
32 putpixel(buffer, x,y, makecol(150,150,150));
33 }
34 }
35 }
36}
37 
38void move()
39{
40 int random = rand()%2;
41 for(int x=0;x <screenWidth;++x)
42 {
43 for(int y=480; y > 0; y--)
44 {
45 int random = rand()%2;
46 if (particle[x][y] == 1)
47 {
48 if(particle[x][y+1]==0)
49 {
50
51 particle[x][y+1]=1;
52 particle[x][y]=0;
53 }
54 else if(particle[x-1][y+1]== 0)
55 {
56 particle[x-1][y+1]=1;
57 particle[x][y]=0;
58 }
59 else if(particle[x+1][y+1]== 0)
60 {
61 particle[x+1][y+1]=1;
62 particle[x][y]=0;
63 }
64 }
65 }
66
67 }
68}
69 
70int main()
71{
72 allegro_init();
73 install_keyboard();
74 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480, 0,0);
75 set_color_depth(16);
76 install_mouse();
77 srand(time(0));
78 character wizard;
79 //monster slime1;
80
81 for(int x=0;x <screenWidth;++x)
82 {
83 particle [x][480] = 2;
84 }
85 for(int x=0;x <screenWidth;++x)
86 {
87 particle [x][479] = 2;
88 }
89
90 buffer = create_bitmap( 640, 480);
91show_mouse(screen);
92 PALETTE pal;
93
94 get_palette(pal);
95wizbit = load_bitmap("wizard.bmp", pal);
96wizbit2 = load_bitmap("wizardl.bmp", pal);
97 while(!key[KEY_ESC])
98 {
99
100 clear_to_color(buffer,makecol(0,0,0));
101 if(key[KEY_RIGHT]){ wizard.right();}
102 if(key[KEY_LEFT]){ wizard.left();}
103 //if(key[KEY_UP]) wizard.jump();
104 if(action == 11 && mouse_b & 1)
105 {
106 particle[mouse_x][mouse_y]=1;
107 particle[mouse_x+1][mouse_y]=1;
108 particle[mouse_x-1][mouse_y]=1;
109 particle[mouse_x][mouse_y+1]=1;
110 particle[mouse_x][mouse_y-1]=1;
111 }
112 if(action == 12 && mouse_b & 1)
113 {
114 particle[mouse_x][mouse_y]=2;
115 particle[mouse_x+1][mouse_y]=2;
116 particle[mouse_x-1][mouse_y]=2;
117 particle[mouse_x][mouse_y+1]=2;
118 particle[mouse_x][mouse_y-1]=2;
119 }
120 //if(key[KEY_DOWN]){ wizard.crouch();}
121 if(key[KEY_1])
122 {
123 action= 11;
124 }
125 if(key[KEY_2])
126 {
127 action= 12;
128 }
129 draw();
130 move();
131 wizard.update_pos();
132 if (wizard.direction == 1){
133 masked_blit(wizbit, buffer, 0,0,wizard.x,wizard.y,8,15);}
134 if (wizard.direction == 2){
135 masked_blit(wizbit2, buffer, 0,0,wizard.x,wizard.y,8,15);}
136 blit(buffer, screen, 0,0,0,0,640,480);
137
138
139
140 }
141}
142END_OF_MAIN();

I can't figure it out, let's see if you all can. :P

Christopher Webb
Member #8,059
December 2006

It's because you put "int particle[640][480];" in the header, so it's instantiating in every .c/.cpp file. Put itin one of the .c files, and put

extern int particle[640][480];

in the header.

TeamTerradactyl
Member #7,733
September 2006
avatar

Christopher's suggestion is correct. I guess I've just never really use constants that way before...

Putting extern int particle[640][480]; in the header file and int particle[640][480]; in either main.cpp or character.cpp seems to fix this.

Durnus
Member #7,997
November 2006
avatar

Hmm, thanks. I've gotta remember this for my next programs too.

LennyLen
Member #5,313
December 2004
avatar

Quote:

Hmm, thanks. I've gotta remember this for my next programs too.

Um, that's exactly what I told you to do in the other thread.

Go to: