473,563 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum-type anonymous structs

If I try compiling this in gcc, it says: "error: request for member
`baz' in something not a structure or union".
Any workarounds or tips on how to make a structure such that it
behaves like an enum but its members can be addressed with '.'?
I don't want to have to do this using #defines, as it would be far too
messy.
code:
static struct {
static const int baz = 1;
} bar;

void foo() {
int x = bar.baz;
}
Jan 3 '08 #1
16 6434
andreyvul <andrey....@gma il.comwrote:
If I try compiling this in gcc, it says: "error: request
for member `baz' in something not a structure or union".
Any workarounds or tips on how to make a structure such
that it behaves like an enum but its members can be
addressed with '.'?
No.
I don't want to have to do this using #defines, as it
would be far too messy.
code:
static struct {
* * static const int baz = 1;
This is not legal C.
} bar;

void foo() {
* * int x = bar.baz;
}
C++ probably has the feature you're looking for.

Judicious naming conventions is as close as you'll come
in C.

--
Peter
Jan 3 '08 #2
On Jan 2, 8:03 pm, Peter Nilsson <ai...@acay.com .auwrote:
andreyvul <andrey....@gma il.comwrote:
If I try compiling this in gcc, it says: "error: request
for member `baz' in something not a structure or union".
Any workarounds or tips on how to make a structure such
that it behaves like an enum but its members can be
addressed with '.'?

No.
I don't want to have to do this using #defines, as it
would be far too messy.
code:
static struct {
static const int baz = 1;

This is not legal C.
} bar;
void foo() {
int x = bar.baz;
}

C++ probably has the feature you're looking for.
I know.
Judicious naming conventions is as close as you'll come
in C.
:(

So I'm stuck to #defines, then?

--
Peter
Jan 3 '08 #3
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<an********@gma il.comwrote:
>If I try compiling this in gcc, it says: "error: request for member
`baz' in something not a structure or union".
Any workarounds or tips on how to make a structure such that it
behaves like an enum but its members can be addressed with '.'?
I don't want to have to do this using #defines, as it would be far too
messy.
code:
static struct {
static const int baz = 1;
} bar;

void foo() {
int x = bar.baz;
}
How does the functionality of enums and structures intersect?

--
Dan Henry
Jan 3 '08 #4
On Jan 2, 8:48 pm, Dan Henry <use...@danlhen ry.comwrote:
How does the functionality of enums and structures intersect?
enums are lists of constants, correct?
What if the list could be accessed using struct format, like
enum.member?
Jan 3 '08 #5
andreyvul <andrey....@gma il.comwrote:
Peter Nilsson <ai...@acay.com .auwrote:
andreyvul <andrey....@gma il.comwrote:
...Any workarounds or tips on how to make a structure
such that it behaves like an enum but its members can
be addressed with '.'?
No.
I don't want to have to do this using #defines, as it
would be far too messy.
code:
static struct {
* * static const int baz = 1;
This is not legal C.
} bar;
>
void foo() {
* * int x = bar.baz;
}
C++ probably has the feature you're looking for.

I know.
Judicious naming conventions is as close as you'll come
in C.

:(

So I'm stuck to #defines, then?
Unlike your #define (which I'm not sure how you see it
operating), naming conventions is established practice,
so it's easier to maintain.

Perhaps if you explain the real problem, in partiuclar why
you need a static member of a struct, then we can suggest
better alternatives.

--
Peter
Jan 3 '08 #6
On Wed, 2 Jan 2008 17:58:29 -0800 (PST), andreyvul
<an********@gma il.comwrote:
>On Jan 2, 8:48 pm, Dan Henry <use...@danlhen ry.comwrote:
>How does the functionality of enums and structures intersect?

enums are lists of constants, correct?
What if the list could be accessed using struct format, like
enum.member?
Well you've got me there. What if? I don't appreciate the value of
accessing "the enum list" using struct (plus member?) format. More
likely is that I don't understand the problem you are trying to solve.
Quite likely is that C does not, other than possibly by preprocessing
means, provide what you are looking for.

--
Dan Henry
Jan 3 '08 #7
Dan Henry wrote:
On Wed, 2 Jan 2008 17:58:29 -0800 (PST), andreyvul
<an********@gma il.comwrote:
>On Jan 2, 8:48 pm, Dan Henry <use...@danlhen ry.comwrote:
>>How does the functionality of enums and structures intersect?
enums are lists of constants, correct?
What if the list could be accessed using struct format, like
enum.member?
Well you've got me there. What if? I don't appreciate the value of
accessing "the enum list" using struct (plus member?) format. More
likely is that I don't understand the problem you are trying to solve.
I suspect that the OP is trying to introduce a name space so that he can
have different flavors of foo by using the enclosing struct name.

What I do is to prefix enumeration constants for an abbreviation associated
with the category:

enum bar {
BAR_FOO,
BAR_BAZ
};

--
Thad
Jan 3 '08 #8
andreyvul <an********@gma il.comwrites:
If I try compiling this in gcc, it says: "error: request for member
`baz' in something not a structure or union".
Any workarounds or tips on how to make a structure such that it
behaves like an enum but its members can be addressed with '.'?
I don't want to have to do this using #defines, as it would be far too
messy.
code:
static struct {
static const int baz = 1;
} bar;

void foo() {
int x = bar.baz;
}
Here's what I get with gcc 4.1.3:

c.c:2: error: expected specifier-qualifier-list before 'static'
c.c: In function 'foo':
c.c:6: error: 'struct <anonymous>' has no member named 'baz'

Are you sure you didn't get an error message on the declaration of
"baz"? (I got no errors when I compiled it as C++.)

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 3 '08 #9
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<an********@gma il.comwrote in comp.lang.c:
If I try compiling this in gcc, it says: "error: request for member
`baz' in something not a structure or union".
Any workarounds or tips on how to make a structure such that it
behaves like an enum but its members can be addressed with '.'?
I don't want to have to do this using #defines, as it would be far too
messy.
code:
static struct {
static const int baz = 1;
} bar;

void foo() {
int x = bar.baz;
}
A few others have questioned why you want to do it, but I haven't seen
anyone post a suggestion like this:

struct bar { int baz; };

static const struct bar bar = { 1 };

void foo(void)
{
int x = bar.baz;
}

You just need to define the type, and than a const initialized (and
static, if you want) object of the type separately.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jan 3 '08 #10

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

Similar topics

3
2148
by: Kenneth | last post by:
I want to define an enum for possible languages in my application. My enum look like this: Public Enum Languages English = 0 Portuguese = 1 End Enum How can i reuse this enum in the rest of my application (in different classes in different projects)?
21
6327
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in different enum definitions? I think it should have been perfectly valid to do that. Its a stupid limitation to have to define disjoint enum symbol...
18
11317
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
8
2234
by: Craig Klementowski | last post by:
All, I've installed the VS 2005 Beta 1 and was trying to build our current product. I get a compile error when enum value is specified with classname::enumname::enumvalue. Seems the compiler does not want the enumname there anymore. This was not a problem with any previous versions of VS. I could not find any help in any of my programming...
5
2656
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer references the Data layer, but I would like to have the enum exposed to all three. Is there a way to trickle down that enum (Like class inheritance...
2
3392
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function and return the name "FirstData"
8
8603
by: kc | last post by:
I'm trying to pull data from a database that I have no control over the structure of. There's a members table with a column for the member's sex. It just stores the sex as M or F. I'd like to create an enum to store the sex. Is there a way that I can create a method in the Enum to convert M to Male and F to Female, or does this method have...
0
1291
by: Ben Finney | last post by:
Howdy all, I've uploaded enum 0.3 to the Cheeseshop. <URL:http://cheeseshop.python.org/pypi/enum/> Enumerations are now sequences, iterable (as before) *and* indexable:: >>> from enum import Enum >>> Weekdays = Enum('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
13
18086
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little detail, the enum is declared as, namespace test{ enum MyEnum{ VALUE1,VALUE2 };
3
12253
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: public enum Days { Sunday };
0
7658
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7631
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6238
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3631
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
912
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.