locked
Visual C++ does not support Nested Enum? RRS feed

  • Question

  • According to an MSDN link I read last night, nested Enums are allowed. Yet Visual Studio 2012 is giving me an error.  I wish I had saved that link, as I am unable to find it now.

    Code:

    namespace Gaia
    {
    	[Windows::Foundation::Metadata::WebHostHidden]
    	public ref class Subscription sealed :
    		public Object
    	{
    	public:
    		
    		enum class NotificationFrequency
    		{
    			EveryTime,
    			Never,
    			Once
    		};
    
    		Subscription(NotificationFrequency notificationFrequency, MessageHandler^ messageHandler, Platform::String^ messageType);
    		Subscription(NotificationFrequency notificationFrequency, MessageHandler^ messageHandler, Platform::String^ messageType, Platform::String^ messageSource);
    
    		virtual Platform::String^ GetType();
    	};
    }

    Error:

    Error 13 error C3989: 'Gaia::Subscription::NotificationFrequency' : a nested type cannot be public e:\misc\gaia\subscription.h 24 1 Gaia

    Is this not allowed?  Making the enum outside the class is not ideal, since it will not be closely associated with the class.


    Land Of Simeria - http://www.stormsonggames.com/los


    Monday, October 15, 2012 8:55 PM

Answers

All replies

  • Yes, Visual C++ allows enums to be nested inside a class.  It has since at least Visual C++ 6 and probably even earlier than that.

    But that says nothing about whether the C++/CX variant of the language used with WinRT has more restrictive rules.  In this case it appears that you cannot nest an enum inside a WinRT class.


    Monday, October 15, 2012 9:38 PM
  • Let me clear few things class is a reserve keyword why are you using it with enum . Second in your case enum  looks like function not enum try to use some valid syntax.

    enum class_NotificationFrequency{EveryTime,Never,Once};

    Thanks


    Rupesh Shukla

    Tuesday, October 16, 2012 5:35 PM
  • Pintu:  "enum class" is valid C++ 11 and supported in VS2012.

    • Edited by SimonRev Tuesday, October 16, 2012 7:31 PM
    • Marked as answer by Elegentin Xie Friday, October 26, 2012 2:45 AM
    Tuesday, October 16, 2012 7:30 PM
  • Let me clear few things class is a reserve keyword why are you using it with enum . Second in your case enum looks like function not enum try to use some valid syntax.
    C++/CLI and C++/CX have enum class, as does also C++11.
     

    David Wilkinson | Visual C++ MVP
    Tuesday, October 16, 2012 7:33 PM
  • Was not aware about that .Thanks for update.

    Thanks


    Rupesh Shukla

    Tuesday, October 16, 2012 7:38 PM