Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

c++ - 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
{
    color1()
    {
        red = green = blue = 0;
    }

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

    float red, green, blue;
};

Any idea ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...