Jump to content
  • Advertisement
Sign in to follow this  
Dark_Glitch

Enum problems

This topic is 3586 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

Hello again everyone. I'm having a bit of a problem getting some functions that use and return enumerated values. Ok, I created a class called TARGET that will be used to identify a target based on a few different enumerated values I have created. Lets say for now there are only two enum values PLAYER and ITEM. When creating a function that has a return type, it must be preceded by said return type when declaring/defining that function, correct? i.e.: int addsomething(int one, int two) or: float getNumber() Well, what if the type is an enum? Shouldn't I be able to write this: (In a header file)
class TARGET
{
private:
enum TargetType { PLAYER, ITEM};
TargetType type;

public:
TargetType GetTargetType() { return TargetType; };

}

However, this doesn't compile...so I decided to define it in the CPP file:
#include "Target.h"

TargetType TARGET::GetTargetType() { return TARGET::TargetType;}

This didn't work either. Now I could've SWORN I can make a function that has a return type of some enum value I created. What am I doing wrong or can this not be done?

Share this post


Link to post
Share on other sites
Advertisement
Instead of creating a new thread I'll post this next question here since it still involves this project.

I have created a class PLAYER that inherits from TARGET.

What is the correct way to set the Target type inside the scope of PLAYER?

Inside of TARGET there is a function

void SetTargetType(TargetType Type);



I thought it would've been: (in Player.cpp)


class PLAYER : private TARGET // this is in Player.h actually

PLAYER::SetTargetType(PLAYER); // I'm telling it to USE this function it should be able to use...



If a class inherits privately from a base class, then it gets ALL the members private, public and protected...well how come I can't get my program to compile?
All I get is a "...\my documents\visual studio 2008\projects\level up\level up\player.cpp(5) : error C2761: 'void TARGET::SetTargetType(TARGET::TargetType)' : member function redeclaration not allowed" error.

Share this post


Link to post
Share on other sites

class TARGET
{
public:
enum TargetType { PLAYER,ENEMY };

private:
TargetType Type;

public:
TARGET(TargetType Type) : Type(Type) { }

TargetType GetType() const { return Type; }
};

class PLAYER : public TARGET
{
public:
PLAYER() : TARGET(TARGET::PLAYER) { }
};


The enum needs to be public if you are returning it from a public method so other parts of the program can use it.

Note though that if you have a class hierachy, the fact that you are also requiring an enum that identifies the type of the subclass suggests that the design is a bit flawed somewhere. Could you post how you intend to use this functionality, as I suspect there would be a better solution using polymorphism.

All-caps CLASSNAMES are a bit odd as well - generally only constants, defines and enumerations are written like this. LeadingCapsLikeThis is more common for class names.

Share this post


Link to post
Share on other sites
Thanks for the response.

Well I wanted to add this to a game like an RPG or something so that when you select an object in game the first thing to be done will be GetTargetType(TargetType type).

Then depending on what the target type is, the player can do several things which I will implement later. I can't even get the enum to work though.

I'm not sure there's a better way, I just never used enum and it looked like it was a pretty neat thing to use. I figured using enums instead of integers would be better.

As for the all caps class names, I'm just using that for now. I plan to change it later. Thanks for the heads up :D.

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!