0

I have problem. I have main.cpp file , where is main game, and few header files like CPlayer.h containing classes. In this classes I have animations, and I used enum to replace current animation number with current animation name like:

Class CPlayer
{
public:
Animation A[2];
int currAnim;

enum animationTitle
{
WALK,
ATTACK,
DESTROY
};
...
}

and I import this class to main. Then I cant do:

player.currAnim = ATTACK;

because enum is in header file. In CPlayer.h this enum works.

Only this works:

player.currAnim = 1;

Is there any way to get around this or I have to add this enum also to main.cpp? I don't want to have hundreds of enums in my main.cpp...(for every object, character...)

Thanks

Barcio77
  • 36
  • 8
  • 1
    Out of curiosity, why did you prefix the class name with "C"? Did you learn this from Microsoft? Will you be consistent and prefix structs with 'S" and integers with 'i'? – Thomas Matthews Mar 14 '14 at 20:24
  • I didn't work for Microsoft. I'm 17. You think this is bad idea? How will I know that is class? – Barcio77 Mar 14 '14 at 20:30
  • @Barcio77: Why do you *need* to know that it is a class? It is a type and you use it as such. I've been programming professionally for ten years and I have never found this to be a problem. – Ed S. Mar 14 '14 at 20:44
  • For example if I make class Boat, and I want to make new instance of this class, I can't use name Boat. I must use other name like boat. If I call class CSomething, I will never make instance called CSomething. I can make instance called Something. Maybe this have no sense but I'm looking for my style of calling classes, variables, structs, functions etc. I don't have to remember all the time "is that function or class? Maybe variable. I have to check" – Barcio77 Mar 14 '14 at 22:21

1 Answers1

2

Then I cant do... because enum is in header file

Not true. It doesn't work because the correct syntax would be:

player.currAnim = CPlayer::ATTACK;

You defined the enum inside of your class. Why? Not sure, but that's what you did.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • @Barcio77: Look up "scope resolution" in your favorite C++ reference. It will help explain. – Thomas Matthews Mar 14 '14 at 20:26
  • Why I shouldn't define enum in my class? – Barcio77 Mar 14 '14 at 20:27
  • @Barcio77: It's not that you *shouldn't*, I just don't see *why* you would in this case. Regardless, your issue is one of scope and syntax. – Ed S. Mar 14 '14 at 20:29
  • I did this because almost every enemy in game will have lots of animations, and I don't want to look every 5 secs which animation has which number of current character. – Barcio77 Mar 14 '14 at 20:32
  • @Barcio77: What does that have to do with the scope of your enum? – Ed S. Mar 14 '14 at 20:41
  • Maybe this is because of I was programming in Actionscript 3...I was used to have names of animations for every object – Barcio77 Mar 14 '14 at 22:26