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,
val3
} enum_var;
When I try to reference the variable, enum_var, in data.h the compiler
does not like it.
I have tried multiple ways of referencing it but I fail each time so
there is clearly something I do not understand about enum or extern or
god knows what.
So I guess to sum up my question is how do I reference the variable
enum_var so that I can include it in each file.
All help would be appreciated.
Thank You,
Charlie 2 4764
Charlie wrote:
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.
Good stuff. (You're not /referencing/ the variables; you're /declaring/
them, saying that they exist.)
Note: /don't use global variables unless you have to/. They're an
easy way to get your kickers in a twinge. You know the way egg-white
is nice and fluid before cooking, but cooking it -- cross-connecting
all those protein strands -- makes it, if not actually /hard/, much
stiffer and not a fluid? Global variables are like that, they make
it harder (and harder and harder) to rearrange your program as time
goes by and each variable cross-connects to more and more bits of
code until your trapped in a maze of twisty little codelines all
almost, but not quite, completely different.
I define an enum and variable of that type in data.c,
enum myenum
{
val1,
val2,
val3
} enum_var;
That enum isn't visible in `data.h` and so not to anything that includes
it.
When I try to reference the variable, enum_var, in data.h the compiler
does not like it.
Good compiler, here's a biscuit.
I have tried multiple ways of referencing it but I fail each time so
there is clearly something I do not understand about enum or extern or
god knows what.
So I guess to sum up my question is how do I reference the variable
enum_var so that I can include it in each file.
All help would be appreciated.
Put the declaration in the header file, same as you do for all the
other variables.
--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
Charlie wrote:
>
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,
val3
} enum_var;
When I try to reference the variable, enum_var, in data.h the compiler
does not like it.
I have tried multiple ways of referencing it but I fail each time so
there is clearly something I do not understand about enum or extern or
god knows what.
So I guess to sum up my question is how do I reference the variable
enum_var so that I can include it in each file.
All help would be appreciated.
It's OK to define typedefs and enum types in a header file.
Not OK for objects and functions
This would be OK in a header file:
enum myenum {
val1,
val2,
val3
};
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Ratliff |
last post by:
I have a few global variables in my app. They are defined in the main
application class file and declared (extern) in a header which can be
included by anyone who would want to use these variables.
Two of the globals are pointers which are initialized at runtime. The others
are classes whose values are initialized in the global space. When I had
just the pointers, I didn't need to include the extern header in the
defining class file. But...
|
by: Chris |
last post by:
I've lurked around long enough... Time to interract =)
I'm trying to make sense of the following. I can't quite wrap my head
around what this is actually doing:
-------------
typedef enum { DOUBLE_LIST, INT_LIST } DATA_TYPE;
typedef struct {
DATA_TYPE type;
|
by: ccwork |
last post by:
Hi all,
I am reading "C: A Reference Manual" 4th ed and I get lost for the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class
specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the
object defined in somewhere, and this "somewhere" object does not have
the storage-class specifier...
|
by: Tapeesh |
last post by:
I would like to know what is the expected behaviour of C compilers when
an extern decleration is intialized.
When the following code is compiled using gcc
//File extern.c
int arr ;
int a ;
|
by: Richard |
last post by:
Okay gang,
This should be simple but apparently it's not... I want
to use the System.DayOfWeek enum to create and access an
array of objects with one object for each day of the
week. I'd like the array declaration to be strongly typed
to the enum and I'd like to use the enum to access entries
in the array. I'd like to write code something like this:
#using System;
| |
by: Martin Jørgensen |
last post by:
Hi,
Problem:
========
Some of my output functions are beginning to take pretty many
arguments... I mean.... We're talking about 10-15 arguments :-)
So I thought to myself, perhaps this is beginning to get out of hands if
I continue to put in extra functionality in the (file-writing)
output-functions....
|
by: Charlie |
last post by:
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:
|
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
{
|
by: Chris Cahoon |
last post by:
Hello,
I am trying to include a header file which was written in C, but my
project is in C++. Even when I wrap the include statement or the
entire header files with extern "C" (used according to the
instructions on c++-faq-lite), I get the same error, which is this:
invalid conversion from `int' to `DmtxDirection'
where DmtxDirection is an enum with integer values inside (obviously).
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |