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(void);
> private:
> DISPLAYMODE GetDisplayMode();
>
> };
>
> CDisplayUtil::CDisplayUtil()
> {
>
> }
>
> CDisplayUtil::~CDisplayUtil()
> {
>
> }
>
> DISPLAYMODE CDisplayUtil::GetDisplayMode()
> {
> INT nWidth = GetSystemMetrics(SM_CXSCREEN);
> INT nHeight = GetSystemMetrics(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:

ISPLAYMODE CDisplayUtil::GetDisplayMode(){...}
2).
if(nHeight > nWidth)
return CDisplayUtil:

ORTRAIT;
if(nHeight < nWidth)
return CDisplayUtil::LANDSCAPE;
return CDisplayUtil::SQUARE;
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