AggrorJorn Posted May 13, 2012 Share Posted May 13, 2012 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. Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted May 13, 2012 Share Posted May 13, 2012 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 1 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted May 13, 2012 Author Share Posted May 13, 2012 Thanks for the help paratoxic. *I added the aLabal:: in front of it to recude the amount of errors. And yes it is inside a class. Quote Link to comment Share on other sites More sharing options...
Mumbles Posted May 13, 2012 Share Posted May 13, 2012 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() Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
Roland Posted May 13, 2012 Share Posted May 13, 2012 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; .... .... .... } } Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
AggrorJorn Posted May 14, 2012 Author Share Posted May 14, 2012 Thanks for the reply Mumbles and Roland. So enum Enum{} can then be used as a variable with TextPosition::Enum& pos . Fascinating! Quote Link to comment Share on other sites More sharing options...
Roland Posted May 14, 2012 Share Posted May 14, 2012 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. Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Recommended Posts
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.