Reputation: 10026
I'm currently trying to break a large "all-in-one-file" program into separate modules in order to make it easier to work with. My problem is that the original (all in one file) version of the program used an enum declaration. I want to continue to use this declaration in the new version of the program that is split into multiple classes, but I'm not sure the best way to do it.
The easiest solution that came to mind was to put the enum block in a header file and to try and use it as a global variable (I know global variables are generally bad, is this an appropriate use?) However, that generated a bunch of "error C4430: missing type specifier - int assumed. Note: C++ does not support default-int".
I realize this is similar to questions that have been asked before. However, all the answers I've been able to find are a bit too technical or not quite what I'm looking for.
My Global.h file and the file where the errors are occurring are included below.
#ifndef GLOBAL_H_
#define GLOBAL_H_
#include <SDL.h>
#include "LTexture.h"
#include "LButton.h"
enum LButtonSprite
{
BUTTON_SPRITE_MOUSE_OUT = 0,
BUTTON_SPRITE_MOUSE_OVER_MOTION = 1,
BUTTON_SPRITE_MOUSE_DOWN = 2,
BUTTON_SPRITE_TOTAL = 2
};
.
.
#endif GLOBAL_H_
...
#ifndef LBUTTON_H
#define LBUTTON_H
#include <SDL.h>
#include "Global.h"
class LButton
{
public:
//Initializes internal variables
LButton();
//Sets top left position
void setPosition(int x, int y);
//Handles mouse event
void handleEvent(SDL_Event* e);
//Shows button sprite
void render();
LButtonSprite getCurrSprite();//Problem is here
private:
//Button Position
SDL_Point mPosition;
//Button Sprite
LButtonSprite mCurrentSprite;//And here
};
#endif
Firstly, what is causing this error? Secondly, can anyone suggest a better way implement what I'm trying to do?
My suspicion is that the compiler is confusing LButtonSprite for a variable. This is based on the colour coding provided by VS (return types are blue, but LButtonSprite is light blue). As can be seen in LButton.h (included below) LButtonSprite should be read as a return type and not as a variable.
Thanks!
Upvotes: 0
Views: 300
Reputation: 8972
Global.h
and LButton.h
each #include
the other. This means the compiler sees the LButton
class before it can see the LButtonSprite
enum, and that's why you are seeing an error message. Either remove the
#include "LButton.h"
from Global.h
or at least put it below the enum
declaration.
Upvotes: 0
Reputation:
This makes no sense. Why are you declaring it extern
when you're defining it? extern
is used for declarations that are defined elsewhere.
Upvotes: 0
Reputation: 148
Without the 'extern' keyword what you've posted is fine.
To avoid naming collisions you could put the enum in a namespace or a class.
Also, if LButton is the only class that uses this enum it could just as well be defined within the class definition of LButton itself.
Upvotes: 1