Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How do I set a class private enumeration from a public member function?  (Read 813 times)

0 Members and 1 Guest are viewing this topic.

RetroRain

  • Newbie
  • *
  • Posts: 14
    • View Profile
The language is C++.

I Googled tons of different results and looked at them, but I can't seem to find a specific answer to what I'm looking for.

I have a Player class, and I want to set his "state", but I want to do it from its public member function, because the state is an enumeration in his private section.  I've tried various different ways, but the compiler keeps giving me errors.  I know how to do it with other values, but when it comes to enumerations, I can't get it to work.  I even tried references, and still no luck.

Here is sample code.  Do you know what I'd have to change to get it work?  Thanks.

#include <iostream>

using namespace std;


class Player
{
   private:
        enum State {IDLE, MOVING};
        State playerState;
   public:
        Player();
        ~Player();
        enum setPlayerState(enum &playerState ps);
        enum getPlayerState();
};

Player::Player()
{
}

Player::~Player()
{
}


enum Player::setPlayerState(enum &playerState ps)
{
   playerState = ps;
}


enum Player::getPlayerState()
{
   return playerState;
}




int main()
{
   Player player;
   player.setPlayerState(MOVING);
   cout << "The player's state is" player.getPlayerState() << "." << endl;
   return 0;
}

I know how to set and get private integer values, but enumerations, I'm having no luck.  I want to be able to type in the parameters player.setPlayerState(IDLE) OR player.setPlayerState(MOVING), but I don't know how to do it from the public member function.

If you know the answer, I just want to say Thank You, and I appreciate your time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How do I set a class private enumeration from a public member function?
« Reply #1 on: November 27, 2018, 07:49:22 am »
The type of your enum is State, so that's what your functions must take/return.

Then obviously the enum has to be public, otherwise only the class itself will be able to use those functions.

Note that this is the SFML forum, so we usually try to focus on SFML and avoid C++ questions ;)
Laurent Gomila - SFML developer

RetroRain

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: How do I set a class private enumeration from a public member function?
« Reply #2 on: December 01, 2018, 06:03:42 am »
Thank you for your response.  I'm sorry for asking a C++ related question.  I'm using SFML at the moment, but I couldn't get this enumeration state code to work, so I typed up this code without the library elements.

And sorry for the late reply.  Been very busy.

Thanks again Laurent for your help. :)