0

In player.h I have enum race {Living, Dead, Nature,None};

In player.cpp I have

race myRace;
    void Player::addPlayer(int x)
 {
    cout<<"added player"<<x<<endl;
    this->karma=0;
    this->myTurn=false;
    myRace=race(4);

 }

So this should , when a player is created make myRace = None.

Now in Main.cpp I want to check a players race, how would i do this? I tried if (player[x].myRace=None) but of course that wont work..

9
  • Does main.cpp include player.h ? Commented Jun 24, 2013 at 7:02
  • What does that mean "of course that wont work" ? Commented Jun 24, 2013 at 7:02
  • yes the linking between classes is correct when i do player[x]. all other items show up just not myRace.... well if it worked, i would not of made post :D Commented Jun 24, 2013 at 7:04
  • 1
    this is a really strange addplayer method. Commented Jun 24, 2013 at 7:04
  • 1
    Using raw integers defeats the point of having the enum. Also, when you try to do your check, did you actually use = instead of ==? Commented Jun 24, 2013 at 7:11

4 Answers 4

3

This should work: myRace = None; and it's the recommended way to use an enum to avoid another mistake that you made, 4 is not a valid integer option for your enum, because Living is 0 and None will be 3.

Sign up to request clarification or add additional context in comments.

Comments

2

Enums should work the way you describe. Unfortunately you only provided modified, unrunnable code and no compiler errors or other output. However, the following code should show you how to use enums: (Live demo)

#include <iostream>

enum Race { None, Living, Dead, Nature}; // always put invalid as the 0 entry.

class Player
{
      uint32_t m_karma;
      bool     m_myTurn;
      Race     m_myRace;

public:
    void addPlayer(int x_)
    {
        std::cout << "added player " << x_ << std::endl;
        m_karma = 0;
        m_myTurn = false;
        m_myRace = None;
    }
};

int main(int argc, const char** argv)
{
    Player fred;
    fred.addPlayer(1);

    return 0;
}

If you have a C++11 capable compiler you can be more specific and use "enum Class" which will force you to qualify enumerations (i.e. Race::None). (Live demo)

#include <iostream>

enum class Race { None, Living, Dead, Nature}; // always but invalid as the 0 entry.

class Player
{
      uint32_t m_karma;
      bool     m_myTurn;
      Race     m_myRace;

public:
    void addPlayer(int x_)
    {
        std::cout << "added player " << x_ << std::endl;
        m_karma = 0;
        m_myTurn = false;
        m_myRace = Race::None;
    }
};

int main(int argc, const char** argv)
{
    Player fred;
    fred.addPlayer(1);

    return 0;
}

"player[x].race = None" won't work because that is assignment, not a test for equality. You don't say why it didn't work, I'm going to assume - if it wasn't just the wrong variable name - because it was private or inaccessible. In that case, just add a member function to return the race:

class Player {
...
public:
    Race getRace() const { return m_myRace; }
    ...
};

2 Comments

ok after going threw this , everything compiles but when i do the getRace() it returns a huge - number. Alli have done is datapie=player[x].getRace(); cout<<datapie;
never mind, figured it out this was correct, i had another issue thansk!
2

The whole point of enums is to do away with magical numbers, so although enums are convertible to integers, getting enums from integers is counter productive. You should write myRace=None, rather than myRace=4.

Secondly unless, you explicitly specify otherwise in your declaration of your enum type, enums starts at 0, so None corresponds to 3 rather than 4.

Thirdly, in C++ you don't have to write this->member_name to access a member variable. If you want to differentiate members from nonmembers, you can save many keystrokes by adopting a much shorter naming convention. (Such as appending _ to member variable names).

Finaly, = in C++ means assignment, NOT equality comparison. if (player[x].myRace=None) effectively means player[x].myRace=None; if (player[x].myRace), i.e. the condition is always true, since player[x].myRace==None==3. Most of the time, the assignment operator inside if conditions is an error and your compiler might warn you about this.

Also, it's kind of weird for a member variable to access your myRace global (and globals are usually a bad idea). If myRace is not a global, then a function can access it only if a pointer or a reference is passed to it as an argument or if myRace is made as a member variable and your function is a method of the same class.

Comments

1

Be careful here ! myRace is not part of Player class. So you will have one instance of myRace, whatever the number of players. You should make myRace part of your class. If you don't, everytime you create a new player, the race changes !

If this is what you want, it should be a static member of your player class, so you could add a static getter tro retrieve it

class Player{

    static int myRace;

public:
    static int GetMyRace(){
        return myRace;
    }
    ...
}

and then, access it like this :

Player::GetMyRace();

4 Comments

Shouldn't it be a non-static field? "myRace" implies the race is mine (the object's), not everyone's.
That's what I am saying... it should be part of Player class if we want as many instances of myRace as Player. But I have some difficlties reading OP's mind here... If it is not part on myRace in the first place, that certainly because he does not want it ?
everytime a player is created, i want that player to have there own race.
@ValekHalfHear It's static in my code cause OP presented an example with a global var. I said it seemed a weird idea, but if it is what he wants, then this could be achieved using a static...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.