Using enum in class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dev_15

    Using enum in class

    Hi, I have this problem that i want to use an enum as a return value
    of a private method in my class, but compiler won't let me use as such

    Code here:

    class CDisplayUtil
    {
    public:
    enum DISPLAYMODE
    {
    LANDSCAPE = -1,
    SQUARE = 0,
    PORTRAIT = 1
    };

    public:
    CDisplayUtil();
    public:

    virtual ~CDisplayUtil(v oid);
    private:
    DISPLAYMODE GetDisplayMode( );
    };



    CDisplayUtil::C DisplayUtil()
    {
    }

    CDisplayUtil::~ CDisplayUtil()
    {
    }

    DISPLAYMODE CDisplayUtil::G etDisplayMode()
    {
    INT nWidth = GetSystemMetric s(SM_CXSCREEN);
    INT nHeight = GetSystemMetric s(SM_CYSCREEN);

    if(nHeight nWidth)
    return PORTRAIT;

    if(nHeight < nWidth)
    return LANDSCAPE;

    return SQUARE;
    }

    What's wrong?

  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: Using enum in class

    On 2007-10-22 11:24, dev_15 wrote:
    Hi, I have this problem that i want to use an enum as a return value
    of a private method in my class, but compiler won't let me use as such
    >
    Code here:
    >
    class CDisplayUtil
    {
    public:
    enum DISPLAYMODE
    All uppercase names are usually reserved for macros. An enumeration is a
    type just like a class, so perhaps EDisplayMode would be a more suitable
    name.
    {
    LANDSCAPE = -1,
    SQUARE = 0,
    PORTRAIT = 1
    Again, Landscape, Square, and Portrait might be better names.
    };
    >
    public:
    You do not need to put public before every member, only when you want to
    change visibility.
    CDisplayUtil();
    public:
    >
    virtual ~CDisplayUtil(v oid);
    private:
    DISPLAYMODE GetDisplayMode( );
    };
    >
    >
    >
    CDisplayUtil::C DisplayUtil()
    {
    }
    >
    CDisplayUtil::~ CDisplayUtil()
    {
    }
    >
    DISPLAYMODE CDisplayUtil::G etDisplayMode()
    CDisplayUtil::D ISPLAYMODE CDisplayUtil::G etDisplayMode()

    The enumeration is part of the class.
    {
    INT nWidth = GetSystemMetric s(SM_CXSCREEN);
    What is wrong with the old honest int?
    INT nHeight = GetSystemMetric s(SM_CYSCREEN);
    >
    if(nHeight nWidth)
    return PORTRAIT;
    >
    if(nHeight < nWidth)
    return LANDSCAPE;
    >
    return SQUARE;
    }
    >
    What's wrong?
    --
    Erik Wikström

    Comment

    • dev_15

      #3
      Re: Using enum in class

      Thanks Erik. it just Windows programming they have all
      their types and enums in Uppercase

      Comment

      • robin

        #4
        Re: Using enum in class

        On Oct 22, 5:24 pm, dev_15 <naumansulai... @googlemail.com wrote:
        Hi, I have this problem that i want to use an enum as a return value
        of a private method in my class, but compiler won't let me use as such
        >
        Code here:
        >
        class CDisplayUtil
        {
        public:
        enum DISPLAYMODE
        {
        LANDSCAPE = -1,
        SQUARE = 0,
        PORTRAIT = 1
        };
        >
        public:
        CDisplayUtil();
        public:
        >
        virtual ~CDisplayUtil(v oid);
        private:
        DISPLAYMODE GetDisplayMode( );
        >
        };
        >
        CDisplayUtil::C DisplayUtil()
        {
        >
        }
        >
        CDisplayUtil::~ CDisplayUtil()
        {
        >
        }
        >
        DISPLAYMODE CDisplayUtil::G etDisplayMode()
        {
        INT nWidth = GetSystemMetric s(SM_CXSCREEN);
        INT nHeight = GetSystemMetric s(SM_CYSCREEN);
        >
        if(nHeight nWidth)
        return PORTRAIT;
        >
        if(nHeight < nWidth)
        return LANDSCAPE;
        >
        return SQUARE;
        >
        }
        >
        What's wrong?
        Once you define an enum inside a class, this class also defines a
        namespace meanwhile. Therefore, here if you want to use DISPLAYMODE
        enum, you should specify its namespace, that is, the class name in
        which this enum is defined.

        The correct lines of code should be:

        1). CDisplayUtil::D ISPLAYMODE CDisplayUtil::G etDisplayMode() {...}
        2).
        if(nHeight nWidth)
        return CDisplayUtil::P ORTRAIT;
        if(nHeight < nWidth)
        return CDisplayUtil::L ANDSCAPE;
        return CDisplayUtil::S QUARE;

        The function declaration of
        private:
        DISPLAYMODE GetDisplayMode( );
        does not need to go with the class name because this function
        declaration is still inside the class CDisplayUtil scope, and the
        definition of DISPLAYMODE is visible. But when you define the member
        function after the closing brace('}') of class CDisplayUtil, which
        means you go out of the scope of CDisplayUtil and into the global
        scope now, the DISPLAYMODE becomes invisible, and you need to give the
        full qualified name to help compiler find it.

        Regards,
        -robin

        Comment

        Working...