Homam Homam - 1 year ago 62
C++ Question

Compile error "'struct' type redefinition" although it's the first definition for it

Everything was working well untill I moved some code from the main file to a new class, then I had the following error:

error C2011: 'color1' : 'struct' type redefinition

struct color1
red = green = blue = 0;

color1(float _red, float _green, float _blue)
red = _red;
green = _green;
blue = _blue;

float red, green, blue;

Any idea ?

Answer Source

If the compiler says it's redefined, then it probably is.

My psychic debugging skills tell me that you moved the struct from a source file to a header file, and forget the include guards in that header, which is then included multiple times in a source file.

EDIT: As a general rule I generally suggest avoiding leading underscores. In some cases (for example followed by a capital letter) they're reserved for the implementation and it's simplest to just never use leading _ instead of hoping you remember all the rules.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download