help with global variable!

G

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.
 
I

I V

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?
 
G

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.
 
J

Jonathan Mcdougall

g said:
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
 
G

GB

g said:
#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
 
R

Rolf Magnus

g said:
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?
 
G

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.
 
I

I V

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top