0

The error I get is .

error: multiple definition of 'enum class Color'

I searched the internet (including here) but I couldn't find a solution. I tried lots of things like deleting the namespaces in header files(still deleted) or putting extern before color(gived error).The only thing seem to work was including everything in the ComposedShape file and including ComposedShape.h in every file but then the classes conflicted somehow.But the thing I know is the header guards should be preventing this so I wrote this version of the code. My files are like this.

--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"

and the cpp files for all of them it gives the error

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    Well, you are defining several times the same thing... If your `.cpp` file includes several of those headers, you will get an error. The header guards are not meant to prevent repeated definitions *from different headers*, only from the current one. What are you trying to do? – Acorn Oct 23 '18 at 19:46
  • 1
    Why are you defining the very same enumeration in all those header files? Why don't you define it in *one single* header file that you include when and where needed? – Some programmer dude Oct 23 '18 at 19:48
  • The functions inside take color as parameter is there a way to not define several times instead of making its own header file for the enum class – paranoid_android Oct 23 '18 at 19:48
  • so just for one enum I should do that.I tougth the header guards will prevent the error – paranoid_android Oct 23 '18 at 19:49
  • 3
    On another and unrelated note, all symbols starting with an underscore followed by an upper-case letter (like for example `_HW2_CIRCLE_H`) are *reserved in all scopes*. See [this old answer](https://stackoverflow.com/a/228797/440558) for more details. – Some programmer dude Oct 23 '18 at 19:49

1 Answers1

0

[Continuing the comments.]

Simply create a header file for your enum class:

--Color.h--
#ifndef _HW2_COLOR_H
#define _HW2_COLOR_H
enum class Color{G,R};
#endif

And include that one everywhere you need it, e.g.:

--Triangle.h--
#ifndef _HW2_TRI_H
#define _HW2_TRI_H

#include "Color.h"
...
#endif

That way, if you include several times Color.h, the definition of Color will appear only once.

Acorn
  • 24,970
  • 5
  • 40
  • 69