Jump to content
  • Advertisement
Sign in to follow this  
Slyfox

Sharing Enums Between Classes

This topic is 3657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm experiencing a serious mental block and I can't figure out how to share an enum between several classes. From what I've read it can be achieved by using namespace but I've tried this and failed. My basic file structure looks like this...
player.h    location.h     EndLocation.h
   |             |                |
   |_____________|________________|
                 |
                 |
               Game.h
                 |
                 |
              Stdafx.h
                 |
                 |
              Main.cpp  

I'm trying to share a enum called position between both player and location. I've tried creating a .h file and defining the namespace EnumShare however after trying to load the file in both player.h and location.h it spat out an error reporting re-definition, which seems rather obvious to me now. Is there a simple way to share an enum between two classes? Am I going about this completely the wrong way?

Share this post


Link to post
Share on other sites
Advertisement
You can define your enum in a header with an include guard:


#ifndef my_enum_H
#define my_enum_H

enum MyEnum { One,Two,Three };

#endif


You can now safely include that in multiple files.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!