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 | |
6 | int particle[640][480]; //Collision array |
7 | |
8 | class character |
9 | { |
10 | public: |
11 | |
12 | void jump(); |
13 | void left(); |
14 | void right(); |
15 | void duck(); |
16 | |
17 | void update_pos(); |
18 | |
19 | int direction; |
20 | |
21 | int x; //X position |
22 | int y; //Y position |
23 | int xvel; //X velocity |
24 | int yvel; //Y velocity |
25 | |
26 | }; |
27 | |
28 | #endif |
character.cpp:
1 | #include "character.h" |
2 | #include <stdlib.h> |
3 | |
4 | |
5 | void 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 | |
27 | void character::left() |
28 | { |
29 | if(xvel > -5) |
30 | { |
31 | xvel--; |
32 | } |
33 | direction = 2; |
34 | } |
35 | void character::right() |
36 | { |
37 | if(xvel < 5) |
38 | { |
39 | xvel++; |
40 | } |
41 | direction = 1; |
42 | } |
43 | void character::jump() |
44 | { |
45 | if(particle[x][y+1] == 0) |
46 | { |
47 | yvel = -10; |
48 | } |
49 | } |
50 | void 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 | |
8 | using namespace std; |
9 | |
10 | int screenHeight = 480; |
11 | int screenWidth = 640; |
12 | BITMAP* buffer; |
13 | |
14 | BITMAP* wizbit; |
15 | BITMAP* wizbit2; |
16 | BITMAP* slime; |
17 | int action = 1; |
18 | |
19 | |
20 | void 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 | |
38 | void 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 | |
70 | int 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); |
91 | show_mouse(screen); |
92 | PALETTE pal; |
93 | |
94 | get_palette(pal); |
95 | wizbit = load_bitmap("wizard.bmp", pal); |
96 | wizbit2 = 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 | } |
142 | END_OF_MAIN(); |
I can't figure it out, let's see if you all can.
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.
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.
Hmm, thanks. I've gotta remember this for my next programs too.
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.