473,473 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Another problem with singleton...

This is the singleton I'm trying to write:

global.h
----------------------------------------
class Global
{
int i;
static Global glob;
//Global(int x): i(x) { } ;
Global();
Global& operator=(Global&); // Disallowed
Global(const Global&); // Disallowed

//some global variables
int myVar;

public:
static Global& instance() { return glob; }

//some set variables
void setmyVar(int);

//some get functions
int getmyVar();

};
-----------------------------------------

global.cpp
-----------------------------------------
#include "global.h"

//Set functions
void Global::setmyVar(int i){myVar = i;}

//Get functions
int Global::getmyVar(){return myVar;}
-----------------------------------------
I'm trying to use the default constructor. But using it,
with this init in application code:
--------------------------------------------
Global Global::glob;
Global& GlobalVariables = Global::instance();
--------------------------------------------

The compiler return a linker error:

[Linker error] undefined reference to `Global::Global()'
If I modify the global.h, using a constructor with arguments, like

Global(int x): i(x) { } ;

instead

Global();

and in application code write this init:

Global Global::glob(1);
Global& GlobalVariables = Global::instance();

it work...

Why???

thx,

Manuel

Jan 7 '06 #1
11 1876
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Manuel wrote:
This is the singleton I'm trying to write:

global.h
----------------------------------------
class Global
{
int i;
static Global glob;
//Global(int x): i(x) { } ;
Global();
Global& operator=(Global&); // Disallowed
Global(const Global&); // Disallowed

//some global variables
int myVar;

public:
static Global& instance() { return glob; }

//some set variables
void setmyVar(int);

//some get functions
int getmyVar();

};
-----------------------------------------


<snip>

Here you declare a constructor Global::Global(). It will be called to
instance the static member 'glob', but you haven't defined it.

- --
Greetings, Thomas Jakob
quicix (at) gmail (dot) com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)

iD8DBQFDwB9jbpGyJtILqbcRArIRAJ9d6hg96uQmwccrZwLocC LN3/BuPgCgxl2+
judyXBGi+qo4gKcTN/yaD88=
=0Yj9
-----END PGP SIGNATURE-----
Jan 7 '06 #2
Thomas Jakob wrote:

Here you declare a constructor Global::Global(). It will be called to
instance the static member 'glob', but you haven't defined it.


To make the experiment I've defined it in header file:

------------------------------------
class Global
{
int i;
static Global glob;
//Global(int x): i(x) { } ;
Global();
Global& operator=(Global&); // Disallowed
Global(const Global&); // Disallowed

//some global variables
int myVar;

public:
static Global& instance() { return glob; }

//some set variables
void setmyVar(int);

//some get functions
int getmyVar();

};

Global::Global(){};
-------------------------------------

calling it in main code with:

Global Global::glob;

the compiler return various errors (global.cpp was the same as previous
post):

global.o(.text+0x0):global.cpp: multiple definition of `Global::Global()'
main.o(.text+0x168):main.cpp: first defined here
global.o(.text+0x6):global.cpp: multiple definition of `Global::Global()'
main.o(.text+0x16e):main.cpp: first defined here
collect2: ld returned 1 exit status

Where is the error :-( ??
Jan 7 '06 #3
Manuel wrote:
calling it in main code with:

Global Global::glob;

the compiler return various errors (global.cpp was the same as previous
post):

global.o(.text+0x0):global.cpp: multiple definition of `Global::Global()'
main.o(.text+0x168):main.cpp: first defined here
global.o(.text+0x6):global.cpp: multiple definition of `Global::Global()'
main.o(.text+0x16e):main.cpp: first defined here
collect2: ld returned 1 exit status

Where is the error :-( ??

Please, help!!!
Jan 8 '06 #4
Manuel wrote:
Thomas Jakob wrote:

Here you declare a constructor Global::Global(). It will be called to
instance the static member 'glob', but you haven't defined it.

To make the experiment I've defined it in header file:

------------------------------------
class Global
{
int i;
static Global glob;
//Global(int x): i(x) { } ;
Global();
Global& operator=(Global&); // Disallowed
Global(const Global&); // Disallowed

//some global variables
int myVar;

public:
static Global& instance() { return glob; }

//some set variables
void setmyVar(int);

//some get functions
int getmyVar();

};

Global::Global(){};


Is this line in the header file? If so, then it will be defined in
every .cpp file that includes the header. Try putting it in a .cpp file
instead, and see if that fixes anything.

Rennie deGraaf
Jan 8 '06 #5
Rennie deGraaf wrote:
Global::Global(){};

Is this line in the header file? If so, then it will be defined in
every .cpp file that includes the header. Try putting it in a .cpp file
instead, and see if that fixes anything.


It work. THANKS!

But there is another thing I dont understand.
If define into header is an error, why the code below work (in the
header file)?

Global::Global(){};

It's a definition, and it's into header too...

thx,

Manuel

Jan 8 '06 #6
Manuel wrote:
Rennie deGraaf wrote:
Global::Global(){};


Is this line in the header file? If so, then it will be defined in
every .cpp file that includes the header. Try putting it in a .cpp file
instead, and see if that fixes anything.

It work. THANKS!

But there is another thing I dont understand.
If define into header is an error, why the code below work (in the
header file)?

Global::Global(){};

It's a definition, and it's into header too...


help!
Jan 8 '06 #7
I think it is because you have declared, but not defined ctor
Global::Global() which is expected to create object Global
Global::glob;

You should define at least some constructor or remove its declaration.

--------------
class Global
{
int i;
static Global glob;
//Global(int x): i(x) { } ;
Global() // <----
{
i = 0;
}
----------

Jan 9 '06 #8

"Manuel" <ma**********************@tin.it> wrote in message
news:43**********************@reader1.news.tin.it. ..
Rennie deGraaf wrote:
Global::Global(){};

Is this line in the header file? If so, then it will be defined in
every .cpp file that includes the header. Try putting it in a .cpp file
instead, and see if that fixes anything.


It work. THANKS!

But there is another thing I dont understand.
If define into header is an error, why the code below work (in the header
file)?

Global::Global(){};

It's a definition, and it's into header too...

thx,

Manuel


Because that's the way C++ works. It's allowed in the class definition (?
is it the definition? I think so).
Jan 9 '06 #9
Manuel wrote:
But there is another thing I dont understand.
If define into header is an error, why the code below work (in the
header file)?

Global::Global(){};

It will work only if it is included in 1 translation unit (i.e., from
within one cpp file). Otherwise you will need to make it inline. To
do that, check out this FAQ, and the one after it:

http://www.parashift.com/c++-faq-lit...s.html#faq-9.7

Hope this helps,
-shez-

Jan 9 '06 #10
Manuel wrote:
But there is another thing I dont understand.
If define into header is an error, why the code below work (in the
header file)?

Global::Global(){};

It will work only if it is included in 1 translation unit (i.e., from
within one cpp file). Otherwise you will need to make it inline. To
do that, check out this FAQ, and the one after it:

http://www.parashift.com/c++-faq-lit...s.html#faq-9.7

Hope this helps,
-shez-

Jan 9 '06 #11
Manuel wrote:
Manuel wrote:
Rennie deGraaf wrote:
Global::Global(){}; Is this line in the header file? If so, then it will be defined in
every .cpp file that includes the header. Try putting it in a .cpp file
instead, and see if that fixes anything.
It work. THANKS!

But there is another thing I dont understand.
If define into header is an error, why the code below work (in the
header file)?

Global::Global(){};

It's a definition, and it's into header too...


help!


I suspect you will find that the following link will help you solve (or
get help solving) your problem:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

It's no longer clear what your problem is. You need to restate it in
full so that folks have an opportunity to understand what you are
asking.

Best regards,

Tom

Jan 9 '06 #12

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

Similar topics

11
by: Vanessa | last post by:
Hi, I would like to know whether there's any way for me to pass an object by reference to another form? Regards Vanessa
5
by: William Payne | last post by:
Hello, consider the following two classes (parent and child): #ifndef SINGLETON_HPP #define SINGLETON_HPP #include <cstddef> /* NULL */ template <typename T> class Singleton {
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
2
by: Eugene | last post by:
Hi, I got a singleton class. Then in another class (CA), i would declare a variable with this singleton class and get a reference to it. I would have another class (IA1) that would inherit from CA...
4
by: jaYPee | last post by:
I know how to open a form from another form. Say open form2 from form1 using a command button. But my problem is everytime I clicked the button it open again another instance of that form. ...
11
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the...
12
by: Rob | last post by:
Let's say you open Form1 that contains TabControl1 There are several tabs on TabControl1 Now you open a new Form2 that contains a User Control How can you determine the Selected tab in Form1...
10
by: =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post by:
Hi, I have created a Singleton class to provide some database functionality in my mobile application. I have a public class called Utility which performs various operations on data. Is it ok to...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
1
by: =?Utf-8?B?U2FpbXZw?= | last post by:
Hello Marc Gravell and Machin. Good Day. Why your not using an Instance in the form and yet your using a New Form? Im using it because I want to open a single form only. If Im using a New...
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,...
1
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...
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,...
1
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...
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?
0
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...

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.