Avatar of IssacJones
IssacJones asked on

Enumerations problem

Hiya

I have a enumeration defined in a header file e.g.

enum HelloGoodbye {HELLO, GOODBYE};

I have also created a function which returns this enumeration e.g.

HelloGoodbye MyClass::SaySomething(void)
{
    return GOODBYE;
}

This works but when I try something like

void MyOtherClass::DoSomething(void)
{
    switch (SaySomething){
       case GOODBYE:
         break;
       case HELLO:
         break;
}

I get the following compile error:

Error      1      error C2065: 'GOODBYE' : undeclared identifier

I have added an include in this file so the enumeration is read so I can't quite see what I'm doing wrong.

Can anybody help?



System Programming

Avatar of undefined
Last Comment
IssacJones

8/22/2022 - Mon
Zoppo

Hi IssacJones,

is that enum within a class declaration (MyClass)?

If so it need to be public and you need to use it with that class-name outside, i.e.:

void MyOtherClass::DoSomething(void)
{
    switch (SaySomething){
       case MyClass::GOODBYE:
         break;
       case MyClass::HELLO:
         break;
}

Hope that helps,

ZOPPO
SOLUTION
alb66

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
IssacJones

Hi Zoppo/alb66

The enumeration is global. The funncy thing is that there are other classes where I use similar enumerations and they work fine. So far it is only this class that there are problems.

Zoppo

Hm - then it's hard to say - maybe you could post the relevant parts of your original code?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
alb66

You miss the end bracket, by I think that it isn't the original code...

void MyOtherClass::DoSomething(void)
{
    switch (SaySomething){
       case GOODBYE:
         break;
       case HELLO:
         break;
   }                       <------------------
}
ASKER
IssacJones

Hiya alb66

The code I wrote was merely pseudo code so the bracket isn't the problem.

I'm going to have another look at the code and see if I can work out why some parts work and others don't.
ASKER
IssacJones

The interesting thing is that in the same file that the switch statement throws up the error, the code

HelloGoodbye MyClass::SaySomething(void)
{
    return GOODBYE;
}

works! That is, it recognies GOODBYE as a HelloGoodye enumeration.

I have now moved the switch statement to the same class i.e.

void MyClass::DoSomething(void)
{
    switch (SaySomething){
       case GOODBYE:
         break;
       case HELLO:
         break;
    }
}

and the same thing happens.

How can it be that the enumeration works in SaySomething but not in DoSomething?

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
alb66

You miss () in SaySomething call. It depends by pseudo code? If yes, can you post real code?

void MyClass::DoSomething(void)
{
    switch ( SaySomething() ){    <----------------------
       case GOODBYE:
         break;
       case HELLO:
         break;
    }
}
ASKER
IssacJones

This is very strange!!!!

I tried adding the line

int x = GOODBYE;

and I got the error: Undeclared identifier.

However, I looked in the list of enumerations I have in the header file and found one with

enum MyOwnList { DOWN, UP };

and then wrote

int x = DOWN;

and it compiled!!!!!!!!!!!!!!!!!!!!!!!!!!!

Why will one enumeration not work but another will?

ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
alb66

May be you have more the one .h file in your file system where you define the enumerations. Pheraps you modify one file while your .cpp file includes another one.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
IssacJones

nope, only one header file has it.

alb66

Can you post it?
ASKER
IssacJones

not really. It contains company sensitive information :(
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
IssacJones

I have noticed that the header file that contains all the enumerations is called several times. As it is global, is there somewhere I can put the include so it is only done once?
alb66

#pragma once  // the first line of the file
ASKER
IssacJones

hi alb66

This is already there.

However, following Zoppo's suggestion i.e.

#pragma message ( "Enum 'HelloGodbye' compiled!" )

This message appears several times during the compilation. As such, even with #pragma once, isn't this being done several times?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
IssacJones

Hi guys

I've discovered the problem - a silly mistake with naming conventions. Everybody helped so I'm going to allocate thep points accordingly.