Jump to content

Recommended Posts

Posted

Hello everyone,

 

I have a beginners problems with C++ enums and switch.

 

My header has in the public section the following:

enum TextPosition
{
 Center,
 TopLeft,
 TopRight,
 BottomLeft,
 BottomRight
};
TextPosition textPosition;

 

Now the CPP initializes the textPosition variable in the constructor.

#include "aLabel.h"
aLabel::aLabel(void)
{
textPosition = aLabel::TextPosition.Center;
}

 

However during a switch case I get the message Unexpected type "aLabel::TextPosition".

case aLabel::TextPosition.Center:

 

What am I doing wrong here? Thanks in advance for the help.

Posted

First one question is ,why you write "aLabel::TextPosition" and not only "TextPosition..." .Is the enum inside a class?

All is right only the line "case aLabel::TextPosition.Center:" is wrong.You have to write in both cases the following:

#include "aLabel.h"
aLabel::aLabel(void)
{
textPosition = aLabel::TextPosition::Center;
}

and

case aLabel::TextPosition::Center:

 

Only a little mistake :)

  • Upvote 1
Posted

The other alternative is to remove the name from the enum, since it is already encapsulated by the class

 

class aLabel
{
   public:
   enum
   {
       TP_Center = 0,
       TP_TopLeft,
       TP_TopRight,
       TP_BottomLeft,
       TP_BottomRight
   };
   int textPosition;
};

 

switch(textPosition)
{
case TP_Center:
   //Blah blah - do stuff
   break;
case TP_TopLeft:
   //More blah blah-ing
}

 

 

If the switch occurs outside an aLabel function, then just change the "case TP_Center:" to "case aLabel::TP_Center:" and obviously change the switch as well to either, (object instance).textPosition, or if text position isn't public (object instnace).getTextPosition()

LE Version: 2.50 (Eventually)

Posted

Hehe... just to add another possibility you can remove the enum from the class and get something like this. This is the way I normally do it. Better? Well I don't know. Its just yet another take on this.

 

namespace TextPostion
{
    enum Enum
    {
         Center,
         TopLeft,
         TopRight,
         BottomLeft,
         BottomRight
    };
}


class SomeClass
{
....
....
....
   void SomeMethod( const TextPosition::Enum& pos )
   {
         switch( pos )
         {
         case TextPosition::Center:
               // ......
               break;

         case TextPosition::TopLeft:
               // ......
               break;

         ....
         ....
         ....
   }
}

Roland Strålberg
Website: https://rstralberg.com

Posted

Yes aggror. Here a complete sample

 


#include "stdafx.h"
#include <iostream>

namespace TextPosition
{
    enum Enum
    {
         Center,
         TopLeft,
         TopRight,
         BottomLeft,
         BottomRight
    };
}


class SomeClass
{
   TextPosition::Enum _pos ;

public:
   SomeClass()
   :    _pos( TextPosition::BottomLeft )
   {}

   void SomeMethod( const TextPosition::Enum& pos )
   {
       switch( pos )
       {
       case TextPosition::Center:
           std::cout << "I'm at Center" << std::endl;
           break;

       case TextPosition::TopLeft:
           std::cout << "I'm at TopLeft" << std::endl;
           break;
       }
   }
};

int main(int argc, char* argv[])
{

   SomeClass  a ;

   a.SomeMethod( TextPosition::Center ) ;
   a.SomeMethod( TextPosition::TopLeft ) ;

   return 0;
}

 

with this output of course.

 

Roland Strålberg
Website: https://rstralberg.com

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...