Compilation error "structure type override", although this is the first definition for it

Everything worked fine until I moved the code from the main file to a new class, then I had the following error:

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

struct color1 { color1() { red = green = blue = 0; } color1(float _red, float _green, float _blue) { red = _red; green = _green; blue = _blue; } float red, green, blue; }; 

Any idea?

+13
c ++
source share
4 answers

If the compiler says that it is overridden, then this is possible.

My mental debugging skills tell me that you moved the structure from the source file to the header file and forgot to include the defenders in that header, which is then included several times in the source file.

EDIT: As a rule, I usually suggest avoiding leading underscores. In some cases (for example, followed by a capital letter) they are reserved for implementation, and the easiest way is to simply use the leading _ instead of hoping that you will remember all the rules.

+51
source share

From the snippet above, I canโ€™t understand that something is wrong.

But usually this error means that you include the same header files several times. Donโ€™t you forget to add standard protection files for the included files?

 #ifndef MY_HEADER_FILE_ #define MY_HEADER_FILE_ // here is your header file code #endif 
+15
source share

You may have a structure definition in the header file. Let

  #pragma once 

at the beginning of the header where the structure is defined, it solves the problem.

+4
source share

I had the same problem, and, fortunately, I did not think for a long time that this was just a stupid mistake.

The fact is that I had a backup of my project on another drive (D :), but all the code was installed on drive C: when the full path was explicitly specified. I created it along the C: path and always used this path, but accidentally opened a project from D and thought it was the same thing, so when it was compiled it turned on twice because in some cases it included code from C: the path and others from D: way.

0
source share

All Articles