Multiple struct definitions

Oct 15, 2011 at 2:04pm
I have a class with a class.h and i have a global.h (witch hold global macros and a struct) The global.h is included both in the main.cpp and the class.cpp and this give me the error:
"struct carbon * carbs" (?carbs@@3PAUcarbon@@A) already defined in creature.obj

This is what creating all the fuzz i think:

1
2
3
4
5
6
7
8
9
10
11
12

struct carbon
{
	int holdingId;
	bool state;
	float x_pos; 
	float y_pos; 
	float z_pos;
};

carbon carbs[CARBS];


It seems like the struct should be outside of globals.h, but how can I then access it both from main.cpp and class.cpp?
Oct 15, 2011 at 3:05pm
One way is to add extern before the declaration in the header file, and to then create the actual variable in one of the .cpp files

http://www.learncpp.com/cpp-tutorial/42-global-variables/

Global variables are not very popular - the discussion at the bottom mentions some alternatives.
Oct 15, 2011 at 4:14pm
Moschops, thanks for replying. I can see from the last example in the link that global variables are so evil, they are capable of starting WW3! This is bad :-) I shal try another way of doing this.
Topic archived. No new replies allowed.