Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...