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