extern enum?

Started by
7 comments, last by En3my 21 years, 8 months ago
hi, i''m getting confused here when putting a small c++ project together. the files i have are main.cpp, class.cpp and class.h. the class will be used to store various data for a sprite, and i will use an enumeration type to hold which direction the sprite is currently facing.
  
// class.h

enum direction_e { NORTH, SOUTH, WEST, EAST };
class CSprite
{
direction_e currDir;
...
}

// class.cpp

#include "class.h"
... // contructor, destructor, member functions etc.


// main.cpp

enum direction_e { NORTH, SOUTH, WEST, EAST };
...
  
I want to use the enum type in both the CSprite class, as well as in main. Obviously, I get a enum type redefinition error when I do like above. Playing around a bit resulted in that I got it working by replacing the enum in the class.h file with "extern enum direction_e". Now everything works, but I don''t fully understand what is going on. I know that extern tells the compiler that a variable is declared in another file. Does it work the same for enumerations? is there another way to do what I''m trying to do, cause I think that my solution is rather ugly! I want to use the enum both in the class as well as in the main code. Thanks!
Advertisement
Here''s another way this could be approched. I got the idea for setting the format of cout. cout.setf(ios::fixed); The word fixed is an enum in the ios class.


  class CSprite{public:  enum direction_e{ NORTH, SOUTH, EAST, WEST };};//in main you could use it like this...CSprite sprite1;sprite1.set_bitmap_direction(CSprite::NORTH);  


Hope this helps

I think, therfore I am.
I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

If you want to use the enum in several source files, simply include the header where it is defined in each one of them. That is the purpose of headers.

Why would you want to declare it in both a header and a source file?
Glass_Knife: Thanks! I like your approach, looks beautiful in my code.

As for using a header file, that is what I was doing at first. Perhaps I was doing it wrong though. I first define the enum in a header file, then I include it in main.cpp and class.h (to be able to use it in the class itself). In class.cpp where I have the member functions I include the class.h definition as well, and I get a link error. I would like to be able to use this approach as well. Any ideas?
Are you sure the link errors weren''t caused by something else? From your description everything sounds ok (enumwise, that is).

Can you post the linker errors you got.

  // enum.henum BOOL { FALSE, TRUE };// class.h#include "enum.h"class A{BOOL flag;...};// main.cpp#include "enum.h"#include "class.h"int main(){ ... }  


This is how I did it at first, and trying it again I get a error C2011: ''enum'' type redefinition. Doing like this I obviously include "enum.h" twice. I have no idea where the linker error came from, cause it doesn''t show up anymore.
BOOL, TRUE and FALSE are already defined if you''re using windows.h. try a less common name for your types.
It didn''t work before because you #included enum.h twice in the same .cpp file.

Once from the .cpp file and once from the class.h file.

Read up on include guards.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
ya, i will add the include guards. thanks! however, i will use Glass_Knife''s method this time, cause it''s very good! the enumeration of BOOL was just used as a short example. thanks all!

This topic is closed to new replies.

Advertisement