473,473 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help with global variable!

g
hello!

I wanna use a global variable,witch is the best way(I don't want to use
a Singleton for an integer)?
I try :

#ifndef DUMY_H_
#define DUMY_H_
int connections;
#endif /*DUMY_H_*/
#include "dumy.h"

int main()
{
connections=9;

..................

#include "DBconnection.h"
#include "dumy.h"
#include <iostream>

DBconnection::DBconnection()
{
std::cout<<connections<<'\n';
for(int a=0;a<connections;a++)
createConnection();
}
but I get this:

../server.o:(.bss+0x0): multiple definition of `connections'
../DBconnection.o:(.bss+0x0): first defined here

any idea?? maybe extern???

thanks.

Apr 29 '06 #1
7 1754
I V
On Sat, 29 Apr 2006 01:45:11 -0700, g wrote:
hello!

I wanna use a global variable,witch is the best way(I don't want to use
a Singleton for an integer)?
The obvious solution is, stop wanting to use a global variable.
I try :

#ifndef DUMY_H_
#define DUMY_H_
int connections;
This is what's causing the multiple definitions.
#endif /*DUMY_H_*/ DBconnection::DBconnection()
{
std::cout<<connections<<'\n';
for(int a=0;a<connections;a++)
createConnection();
}


Why not make connections a member of DBConnection?

Apr 29 '06 #2
g
becouse I want connections to be dynamic,at runtime the user will
choose the number,
actually in main() when the program starts.

any idea?

thanks.

Apr 29 '06 #3
g wrote:
I wanna use a global variable,witch is the best way(I don't want to use
a Singleton for an integer)?
I try :

#ifndef DUMY_H_
#define DUMY_H_
int connections;
#endif /*DUMY_H_*/


If that's in a header getting included multiple times, you will get
linker errors. Make it extern and define it only once.

// dumy.h
#ifndef DUMY_H_
#define DUMY_H_
extern int connections; // declaration
#endif /*DUMY_H_*/

// main.cpp
int connections; // definition

// test.cpp
# include "dumy.h"

// only has the declaration here
Also, you may want to review your design. Does "connections" really
need to be a global variable? If so, can you hide it somewhere safe,
like in an already existing singleton? A loose global may be
complicated to track and manage correctely.
Jonathan

Apr 29 '06 #4
GB
g wrote:
#ifndef DUMY_H_
#define DUMY_H_
int connections;
#endif /*DUMY_H_*/
In the header, at file scope, use

extern int connections;

instead of

int connections;

This declares the presence of the int variable to all the cpp files that
include the header. What you had before was effectively defining a new
static variable in each .cpp file with the same name. Hence the multiple
definition error.
but I get this:

./server.o:(.bss+0x0): multiple definition of `connections'
./DBconnection.o:(.bss+0x0): first defined here


In exactly *one* .cpp file, at file scope, add

int connections;

Gregg
Apr 29 '06 #5
g wrote:
becouse I want connections to be dynamic,at runtime the user will
choose the number, actually in main() when the program starts.


Please quote context.
What makes you think a member variable's value cannot be changed at runtime?

Apr 29 '06 #6
g
thanks for your replies guys:-)

the reason I want this deep down and dirty solution :-))
is becouse conections will be used only once during the creation of
connections to db
I will use Singleton,
Singleton<DBConnection>::getInstance->getConection()
so an argument wouldn't be trivial....

and there is an interface and I dont want to change it and from main()
ala chain of responsibilty pass the connection somewhere i can get it.

Apr 29 '06 #7
I V
On Sat, 29 Apr 2006 02:33:01 -0700, g wrote:
becouse I want connections to be dynamic,at runtime the user will
choose the number,
actually in main() when the program starts.

any idea?


It would probably be best to pass connections in to the DBConnection
constructor, then you can ensure that the DBConnection class doesn't get
created before you have specified the number of connections. But if
you can't do that, a static member might work:
/* DBConnection.h */

class DBConnection
{
static int connections;
public:
static void set_connections(int n);
DBConnection();
};

/* DBConnection.cpp */

/* Set this to some sensible default */
int DBConnection::connections = 4;

static void DBConnection::set_connections(int n)
{
connections = n;
}

DBConnection::DBConnection()
{
....
}
Another way to design this (although this might be too big a change for
you to make easily at this point), would be to use a pool rather than a
singleton, so that each DBConnection is responsible for one connection,
and the pool creates as many DBConnections as you need.
Apr 30 '06 #8

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

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
2
by: simon | last post by:
hello, what i'm looking to do is store the path of the app on a the server for reuse in the site. my thoughts so far are... -make a key in the web.config file -retrieve the value in globals.asax...
3
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
8
by: yinglcs | last post by:
Hi, I read this article about global variable in c: http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html But I have a few questions 1. how can I declare the global variable...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
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
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,...
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.