枚举的多重定义 class 'classname'
Multiple definition of enum class 'classname'
我得到的错误是。
error: multiple definition of 'enum class Color'
我搜索了互联网(包括此处),但找不到解决方案。
我尝试了很多事情,比如删除头文件中的名称空间(仍然被删除)或将 extern 放在颜色之前(给出错误)。唯一似乎有效的是包括 ComposedShape 文件中的所有内容并在每个文件中包括 ComposedShape.h 但是然后 类 冲突 somehow.But 我知道头文件保护应该阻止这种情况所以我写了这个版本的代码。
我的文件是这样的。
--Circle.h--
#ifndef _HW2_CIRCLE_H
#define _HW2_CIRCLE_H
enum class Color{G,R};
...
#endif
--Triangle.h--
#ifndef _HW2_TRI_H
#define _HW2_TRI_H
enum class Color{G,R};
...
#endif
--Rectangle.h--
#ifndef _HW2_RECT_H
#define _HW2_RECT_H
enum class Color{G,R};
...
#endif
--ComposedShapes.h--
#ifndef _HW2_CS_H
#define _HW2_CS_H
...
#endif
--main.cpp--
#inlude "ComposedShapes.h"
以及所有这些的 cpp 文件
它给出了错误
[继续评论。]
只需为您的 enum class
:
创建一个头文件
--Color.h--
#ifndef _HW2_COLOR_H
#define _HW2_COLOR_H
enum class Color{G,R};
#endif
并在您需要的任何地方添加它,例如:
--Triangle.h--
#ifndef _HW2_TRI_H
#define _HW2_TRI_H
#include "Color.h"
...
#endif
这样,如果你包含多次Color.h
,Color
的定义将只出现一次。
我得到的错误是。
error: multiple definition of 'enum class Color'
我搜索了互联网(包括此处),但找不到解决方案。 我尝试了很多事情,比如删除头文件中的名称空间(仍然被删除)或将 extern 放在颜色之前(给出错误)。唯一似乎有效的是包括 ComposedShape 文件中的所有内容并在每个文件中包括 ComposedShape.h 但是然后 类 冲突 somehow.But 我知道头文件保护应该阻止这种情况所以我写了这个版本的代码。 我的文件是这样的。
--Circle.h--
#ifndef _HW2_CIRCLE_H
#define _HW2_CIRCLE_H
enum class Color{G,R};
...
#endif
--Triangle.h--
#ifndef _HW2_TRI_H
#define _HW2_TRI_H
enum class Color{G,R};
...
#endif
--Rectangle.h--
#ifndef _HW2_RECT_H
#define _HW2_RECT_H
enum class Color{G,R};
...
#endif
--ComposedShapes.h--
#ifndef _HW2_CS_H
#define _HW2_CS_H
...
#endif
--main.cpp--
#inlude "ComposedShapes.h"
以及所有这些的 cpp 文件 它给出了错误
[继续评论。]
只需为您的 enum class
:
--Color.h--
#ifndef _HW2_COLOR_H
#define _HW2_COLOR_H
enum class Color{G,R};
#endif
并在您需要的任何地方添加它,例如:
--Triangle.h--
#ifndef _HW2_TRI_H
#define _HW2_TRI_H
#include "Color.h"
...
#endif
这样,如果你包含多次Color.h
,Color
的定义将只出现一次。