How to pass an enum to a function

A

Angus

Hello

I have an enum in a class like this:

enum MessageType
{
GET, //get message (get SAF sequence no.)
RESET, //reset sequence no.
UNKNOWN //unrecognised message
};

In the private section.

And a function like this:

CreateMessage(enum MessageType eType);

But when using the function how do I call it?

eg tried
CreateMessage(GET) and CreateMessage(MessageType.GET) and got error
C2228: left of '.GET' must have class/struct/union type

How do I pass enum to the function?
 
N

Nick Keighley

I have an enum in a class like this:

enum MessageType
{
GET, //get message (get SAF sequence no.)
RESET, //reset sequence no.
UNKNOWN //unrecognised message
};

In the private section.

the what?
And a function like this:

CreateMessage(enum MessageType eType);

in C++ you can omit the "enum"

But when using the function how do I call it?

eg tried
CreateMessage(GET) and CreateMessage(MessageType.GET) and got error
C2228: left of '.GET' must have class/struct/union type

How do I pass enum to the function?

you should post a complete compilable example. This compiles:-

enum MessageType
{
GET,
RESET,
UNKNOWN
};

void CreateMessage (MessageType eType)
{
}

int main ()
{
CreateMessage (GET);
return 0;
}
 
I

Ian Collins

Angus said:
Hello

I have an enum in a class like this:

enum MessageType
{
GET, //get message (get SAF sequence no.)
RESET, //reset sequence no.
UNKNOWN //unrecognised message
};

In the private section.

And a function like this:

CreateMessage(enum MessageType eType);

drop the "enum".
But when using the function how do I call it?

eg tried
CreateMessage(GET) and CreateMessage(MessageType.GET) and got error
C2228: left of '.GET' must have class/struct/union type

How do I pass enum to the function?
You are not passing the enum, but a value from that enum. To do this
with an enum that is nested in a class, the enum must be public.
 
J

jalqadir

Get rid of the enum keyword from the function declaration and
implementation, that'll do the trick.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hello

I have an enum in a class like this:

enum MessageType
{
GET, //get message (get SAF sequence no.)
RESET, //reset sequence no.
UNKNOWN //unrecognised message
};

In the private section.

And a function like this:

CreateMessage(enum MessageType eType);

But when using the function how do I call it?

eg tried
CreateMessage(GET) and CreateMessage(MessageType.GET) and got error
C2228: left of '.GET' must have class/struct/union type

How do I pass enum to the function?

First of, as Ian Collins pointed out, you must declare the enum public,
second you have to tell the compiler that you want the enum declared in
the class by prefixing with the class name (a class acts as (or is?) a
namespace):

class Message
{
public:
enum MessageType { Get, Reset, Unknown };
};

Message CreateMessage(Message::MessageType type);
^^^^^^^^^
int main()
{
Message msg = CreateMessage(Message::Get);
^^^^^^^^^
}
 
N

nallayan77

First of, as Ian Collins pointed out, you must declare the enum public,
second you have to tell the compiler that you want the enum declared in
the class by prefixing with the class name (a class acts as (or is?) a
namespace):

class Message
{
public:
enum MessageType { Get, Reset, Unknown };

};

Message CreateMessage(Message::MessageType type);
^^^^^^^^^
int main()
{
Message msg = CreateMessage(Message::Get);
^^^^^^^^^

}

Hi,

enum MSG_TYPE { Get, Reset, Unknown }; //you can have this outside the
class

Then you can have like this.
class Message
{
private:
MSG_TYPE MessageType;
....
......
};

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top