473,770 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum types and extern

I tried to post this before and I apologize if I am repeating myself,
but I do not see the post anywhere.

But anyway,

I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:

enum myenum
{
val1,
val2,
val3,
} enum_var;

When I try to reference enum_var in data.h I always get an error. I
have tried numerous ways of referencing it and defining it but I
always seem to get a compiler error.

So how can I externally reference enum_var in data.h so that I can use
enum_var in my program? And if I am totally missing the point on
this, if someone could clear up my confusion about enum or extern that
would be great.

Thank you,

Charlie

Apr 24 '07
10 3037
On Apr 24, 8:19 am, Charlie <evilzucch...@y ahoo.comwrote:
I tried to post this before and I apologize if I am repeating myself,
but I do not see the post anywhere.

But anyway,

I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:

enum myenum
{
val1,
val2,
val3,
} enum_var;

When I try to reference enum_var in data.h I always get an error. I
have tried numerous ways of referencing it and defining it but I
always seem to get a compiler error.

So how can I externally reference enum_var in data.h so that I can use
enum_var in my program? And if I am totally missing the point on
this, if someone could clear up my confusion about enum or extern that
would be great.
You define types (like enum, struct) in a header file. Typically you
do not
define such things in a .c file (unless it's self containted in the
same file
and it's not needed outside the .c file). The header file is included
in all .c files that need it.

Variables are defined in .c file (these lead to actual object creation
-- storage
allocation etc) -- you don't define variables in a header file.

Thus in your case, you both define a type and define a variable in the
same place --
and it's in the .c file. Split them into two as described above.

In data.h:
enum myenum
{
val1,
val2,
val3,
} ; <--- just defines a type -- so object/storage
allocation happens.

extern enum myenum enum_var; <-- just says somewhere in some .c file
object will be created

In data.c:
enum myenum enum_var; <--- leads to object creation

Sometimes, it helps to use a typedef for the first type definition
(typedef enum {...} myenum_en;)

Karthik
>
Thank you,

Charlie

Apr 26 '07 #11

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

Similar topics

11
37096
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't explicitly say about enum's forward declaration, but one example shows it in 18.2.1, clause 4:
2
5496
by: Sandy | last post by:
I have an enum like this file1.cpp enum e {e1, e2}; extern void fun(); void main() { fun(); }
31
3616
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
12
3360
by: Laurent Deniau | last post by:
If I understand well, an enumeration is only garantee to hold at most an int (6.7.2.2-2). So I would like to know: how to store a long in an enum? enum { p2_31 = 1L << 31 }; // boom how to define a synonym of a constant address?
34
11204
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
12
6723
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 };
2
4765
by: Charlie | last post by:
I use a file to define all of my global variables, data.c, and then I create a header file, data.h, where I reference all the variables with the keyword "extern." I include the header in every file. I define an enum and variable of that type in data.c, enum myenum { val1, val2,
12
22762
by: Charlie | last post by:
I have a file, data.c, where I define all of my global variables. I then have a header file, data.h, which I include in every file in which I reference all of the variables defined in data.c. For the most part, I am having no problem with this. However, I defined an variable of type myenum in data.c: enum myenum {
7
11155
by: bashill.zhu | last post by:
bzhu@TY-PC /h/working/tcplex/ch8/testenum $ cat lexer.h namespace Lexer { enum Token_value; extern Token_value string_value; void get_token(); } bzhu@TY-PC /h/working/tcplex/ch8/testenum
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7460
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.