单身的另一个问题...... [英] Another problem with singleton...
问题描述
这是我想写的单身人士:
global.h
------------ ----------------------------
class Global
{
int i;
static全球glob;
// Global(int x):i(x){};
Global();
Global&运算符=(全球&安培); //不允许
Global(const Global&); //不允许
//一些全局变量
int myVar;
public:
静态全球& instance(){return glob; }
//一些设置变量
void setmyVar(int);
//一些获取函数
int getmyVar();
};
---------------- -------------------------
global.cpp
- ---------------------------------------
#include" ; global.h"
//设置函数
void Global :: setmyVar(int i){myVar = i;}
//获取函数
int Global :: getmyVar(){return myVar;}
------------ -----------------------------
我正在尝试使用默认构造函数。但使用它,
在应用程序代码中使用此init:
----------------------- ---------------------
Global Global :: glob;
Global& GlobalVariables = Global :: instance();
--------------------------------- -----------
编译器返回链接器错误:
[链接器错误]未定义引用` Global :: Global()''
如果我修改global.h,使用带参数的构造函数,比如
Global(int x):i (x){};
而不是
全球();
和申请表代码写这个init:
Global Global :: glob(1);
Global& GlobalVariables = Global :: instance();
它有效...
为什么???
thx,
Manuel
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
推荐答案
----- BEGIN PGP SIGNED MESSAGE -----
哈希:SHA1
Manuel写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Manuel wrote:
这是我的单身人士试着写:
global.h
------------------------------- ---------
class Global
{
int i;
静态全局glob;
// Global(int x):i(x ){};
Global();
Global&运算符=(全球&安培); //不允许
Global(const Global&); //不允许
//一些全局变量
int myVar;
public:
static Global& instance(){return glob; }
//一些设置变量
void setmyVar(int);
//一些获取函数
int getmyVar();
};
---------------------------------------- -
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>
这里你声明一个构造函数Global :: Global()。它将被称为
实例静态成员''glob'',但你还没定义它。
- -
问候,Thomas Jakob
quicix(at)gmail(dot)com
----- BEGIN PGP SIGNATURE -----
版本:GnuPG v1.4.2(MingW32)
iD8DBQFDwB9jbpGyJtILqbcRArIRAJ9d6hg96uQmwccrZwLocC LN3 / BuPgCgxl2 +
judyXBGi + qo4gKcTN / yaD88 =
= 0Yj9
----- END PGP SIGNATURE -----
<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-----
Thomas Jakob写道:
Thomas Jakob wrote:
在这里,您声明一个构造函数Global :: Global()。它将被称为
实例静态成员''glob'',但你没有定义它。
Here you declare a constructor Global::Global(). It will be called to
instance the static member ''glob'', but you haven''t defined it.
要进行实验我已经在头文件中定义了它:
------------------------------ ------
class Global
{
int i;
static Global glob;
// Global(int x):i(x){};
Global();
Global&运算符=(全球&安培); //不允许
Global(const Global&); //不允许
//一些全局变量
int myVar;
public:
静态全球& instance(){return glob; }
//一些设置变量
void setmyVar(int);
//一些获取函数
int getmyVar();
};
Global :: Global(){};
-------------------------------------
在主代码中调用它:
Global Global :: glob;
编译器返回各种错误(global.cpp)与之前的
帖子相同):
global.o(。text + 0x0):global.cpp:`Global :: Global的多重定义()''
main.o(。text + 0x168):main.cpp:首先在这里定义
global.o(。text + 0x6):global.cpp :'Global :: Global()''的多重定义
main.o(。text + 0x16e):main.cpp:首先在这里定义
collect2:ld返回1退出状态
错误在哪里:-( ??
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 :-( ??
Manuel写道:
Manuel wrote:
在主代码中调用它:
Global Global :: glob;
global.o(。text + 0x0):global.cpp:`Global的多重定义:: Global()''
main.o(。text + 0x168):main.cpp:首先在这里定义
global.o(。text + 0x6):global.cpp:`的多重定义Global :: Global()''
main.o(。text + 0x16e):main.cpp:首先在这里定义
collect2:ld返回1退出状态
错误:-( ??
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!!!
这篇关于单身的另一个问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!