473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to pass an enum to a function

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?

Jul 30 '07 #1
5 33426
On 30 Jul, 10:46, Angus <anguscom...@gmail.comwrote:
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;
}
--
Nick Keighley


Jul 30 '07 #2
Angus wrote:
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.

--
Ian Collins.
Jul 30 '07 #3
Get rid of the enum keyword from the function declaration and
implementation, that'll do the trick.
On Jul 30, 2:46 am, Angus <anguscom...@gmail.comwrote:
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?

Jul 30 '07 #4
On 2007-07-30 11:46, Angus wrote:
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);
^^^^^^^^^
}

--
Erik Wikström
Jul 30 '07 #5
On Jul 30, 4:19 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-07-30 11:46, Angus wrote:
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);
^^^^^^^^^

}

--
Erik Wikström
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

Jul 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Yangang Bao | last post by:
I have a simple test program. It doesn't work. I want to know what is the reason and how to fix it. Maybe I should use template function to do it. But I don't know how. Here is the simple...
6
by: _andrea.l | last post by:
I'd like to write a function like: function f(){ ... bla ...} f_example(f); function f_example($function_to_execute) {...bla... $function_to_execute() ...bla....} AND how to pass the...
2
by: Ronnie Smith | last post by:
I am trying to pass a function (method) address to a user control to associate with an event. The small user control which sends events is contained in a larger user control group which is in...
6
by: Minfu Lu | last post by:
I have a problem dealing with passing a function address to a COM callback. I use this COM function for communicating to a hardware. My original project was written in VB. I have converted it to...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
8
by: laredotornado | last post by:
Hi, I want to pass my function myFunc('a', 'b', 'c') as an argument to another function. However, this will not work doStuff('x', 'y', myFunc('a', 'b', 'c'))
1
by: shalini jain | last post by:
Hi, I am facing the problem with eval function. I want to know how to pass a function in eval() when we have to create the function itself. I would better explain my problem with the help of an...
4
by: mwanstall | last post by:
Hi All, I have started tearing my hair out over this problem! I am pulling some data from a table and passing it as variables into a function in Access. One of the variables I'm passing through...
5
by: kjqua | last post by:
Hi all, I am newbie in c# and i have a question. I try to work with serial port. and i will like to configure the serial port by a form I add in a ComboBox.parity some items like: None Odd
10
by: iskhan | last post by:
Is it possible that pass a function name as a argument in an other function? e.g. MainMenu($SubMenu) { //Main menu designing start if(.......&&......||.......&&.......) { $this->$Submenu;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.