473,385 Members | 1,356 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,385 software developers and data experts.

acces to global variabels from more than one class

Hallo,

normally I am not the one who calls a Newsgroup if something does not work.
But now I am frustrated.
I did write a program with a lot classes. I use some gloabl variables which
are in a file called global.h. Now two of my classes access this variables.
One classes changes the values (writes) and another read them out.

So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"

My question now is, if saomebody knows about such a problems and can provide
a way to solve it.

Many Thanks
Jan-Henrik

Jul 22 '05 #1
8 3195
Jan-Henrik Grobe wrote:
Hallo,

normally I am not the one who calls a Newsgroup if something does not work.
But now I am frustrated.
I did write a program with a lot classes. I use some gloabl variables which
are in a file called global.h. Now two of my classes access this variables.
One classes changes the values (writes) and another read them out.
So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"


__declspec is not Standrad C++, so g++ rejects it. After reading the
description of __declspec(selectany) I think, that extern and this
"keyword" is not the same thing. Do you include the global.h - file or
to you declare the two gloabls in each file where you need them?

Please post the part of the code wich makes the problems.
Maybe you forgot to provied include-guards, in case you are including
global.h, but thats only guessing without a piece of code.

Kind regards,
Nicolas
Jul 22 '05 #2

"Nicolas Pavlidis" <ao***********@aon.at> schrieb im Newsbeitrag
news:33*************@individual.net...
Jan-Henrik Grobe wrote:
Hallo,

normally I am not the one who calls a Newsgroup if something does not
work. But now I am frustrated.
I did write a program with a lot classes. I use some gloabl variables
which are in a file called global.h. Now two of my classes access this
variables. One classes changes the values (writes) and another read them
out.


So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"


__declspec is not Standrad C++, so g++ rejects it. After reading the
description of __declspec(selectany) I think, that extern and this
"keyword" is not the same thing. Do you include the global.h - file or to
you declare the two gloabls in each file where you need them?

Please post the part of the code wich makes the problems.
Maybe you forgot to provied include-guards, in case you are including
global.h, but thats only guessing without a piece of code.

Kind regards,
Nicolas


Hallo,

many thanks for the help attempt...my code is a little longer but I found a
good example that shows the problem. In this example the functions
computeTime and output Time are in two different files. The third file is
global.h

global.h:

__declspec(selectany) int globalhour = 0;
__declspec(selectany) int globalminute = 0;
__declspec(selectany) int globalsecond = 0;

void computeTime(void)
{
// imagine a function that gets the actual hour, minute and second

globalhour = hour;
globalminute = minute;
globalsecond = second;
}
void outputTime(void)
{
printf("Now it is exactly %d hours, %d minutes and %d seconds \n",
globalhour,

globalminute,

globalsecond);
}

if I compile my code with these __declspecs I got this error:

"ISO C++ forbits declaration of "__declspec" with no type"

So I am assuming the compiler doesnt know this construction. I am looking
for an alternative for Linux.

Many thanks
Jan-Henrik
Jul 22 '05 #3
Jan-Henrik Grobe wrote:
Hallo,

many thanks for the help attempt...my code is a little longer but I found a
good example that shows the problem. In this example the functions
computeTime and output Time are in two different files. The third file is
global.h

global.h:

[...]

if I compile my code with these __declspecs I got this error:

"ISO C++ forbits declaration of "__declspec" with no type"
Yes, because __declspec is a microsoft specific thing, and not Stdandard
C++.
So I am assuming the compiler doesnt know this construction. I am looking
for an alternative for Linux.


Did you try the follwoing?
#ifndef global_h___
#define global_h___

// define here the gloabal variables.
#endif

That is called include - guard.
Jul 22 '05 #4
So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"
Be sure that the variable is only _defined_ once.

For example, if I have

extern int i;

in my header file, then I should have ONLY AND EXACTLY ONE

int i;

in any of my C source files. As in, pick ONE file to put the "int i;"
in, and DONT PUT IT ANYWHERE ELSE.

"extern int i;" tells the compiler that somewhere there will be a
variable called "i" that is global.

"int i;" tells the compiler "it's right here". If you say "int i;"
multiple places, the compiler will get confused.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017


My question now is, if saomebody knows about such a problems and can provide
a way to solve it.

Many Thanks
Jan-Henrik

Jul 22 '05 #5

"Jan-Henrik Grobe" <j-*******@t-online.de> a écrit dans le message de news:
cr*************@news.t-online.com...
Hallo,

normally I am not the one who calls a Newsgroup if something does not
work. But now I am frustrated.
I did write a program with a lot classes. I use some gloabl variables
which are in a file called global.h. Now two of my classes access this
variables. One classes changes the values (writes) and another read them
out.

So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"


With no code, it is quite difficult and I can only guess. You should put the
definition of global variables in one place only (therefore, NOT in
headers).

// globals.cpp

int my_global=0;

You then only have to declare these globals in a header.

// globals.h

extern int my_global;

To use it, include globals.h which does not define the variable, it only
declares it. globals.cpp defines the global variables only once, so both
the compiler and linker are happy.

Jonathan
Jul 22 '05 #6
On Tue, 04 Jan 2005 16:56:23 +0100, Nicolas Pavlidis
<ao***********@aon.at> wrote in comp.lang.c++:
Jan-Henrik Grobe wrote:
Hallo,

many thanks for the help attempt...my code is a little longer but I found a
good example that shows the problem. In this example the functions
computeTime and output Time are in two different files. The third file is
global.h

global.h:


[...]

if I compile my code with these __declspecs I got this error:

"ISO C++ forbits declaration of "__declspec" with no type"


Yes, because __declspec is a microsoft specific thing, and not Stdandard
C++.
So I am assuming the compiler doesnt know this construction. I am looking
for an alternative for Linux.


Did you try the follwoing?
#ifndef global_h___
#define global_h___


No, don't do this. This violates the C++ standard. Any identifier in
any context with two consecutive underscores is reserved for the
implementation.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #7
Jack Klein wrote:
On Tue, 04 Jan 2005 16:56:23 +0100, Nicolas Pavlidis
<ao***********@aon.at> wrote in comp.lang.c++:

Jan-Henrik Grobe wrote:

Hallo,

many thanks for the help attempt...my code is a little longer but I found a
good example that shows the problem. In this example the functions
computeTime and output Time are in two different files. The third file is
global.h

global.h:


[...]

if I compile my code with these __declspecs I got this error:

"ISO C++ forbits declaration of "__declspec" with no type"


Yes, because __declspec is a microsoft specific thing, and not Stdandard
C++.

So I am assuming the compiler doesnt know this construction. I am looking
for an alternative for Linux.


Did you try the follwoing?
#ifndef global_h___
#define global_h___

No, don't do this. This violates the C++ standard. Any identifier in
any context with two consecutive underscores is reserved for the
implementation.

Sorry *gettingred*
Thank you for the correction, here an includegurad that confirms to the
standard:
#ifndef GLOBAL_H_
#define GLOBAL_H_

#endif

Best regards,
Nicolas
Jul 22 '05 #8
Hallo,

I just wish to say thank you to everyone who helped me. Both varieties
worked for me. The include guard method and the declaration in the header
and initializing in the .cpp.

Many thanks
Jan
Jul 22 '05 #9

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

Similar topics

33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
3
by: VB Programmer | last post by:
How do you define a variable as global in VB.NET 2005? Thanks.
5
by: Mrozu | last post by:
Hi I have frm1. On this form button.Click code for this button is: Dim frm2 as New frm2 frm2.show So after click, frm2 form is shown.
5
by: Brian Cahill | last post by:
Hello, I am trying to create a variable when my form loads which can be used when I click my buttons. Here is my code. What am I doing wrong? Thanks for any help. Private Sub...
4
by: MulderM | last post by:
I have made an DAP (Data Acces Page) and want to link my .mdb file to a webserver. For instance http://www.blabla.nl But I can't get it to work. Please cab someone help me with this ? ...
23
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
3
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.