由于使用静态变量而导致未定义的引用错误
今天早些时候我问了一个关于单例的问题,我在理解我遇到的一些错误时遇到了一些困难。我有以下代码:
Timing.h
class Timing {
public:
static Timing *GetInstance();
private:
Timing();
static Timing *_singleInstance;
};
Timing.cpp
#include "Timing.h"
static Timing *Timing::GetInstance() { //the first error
if (!_singleInstance) {
_singleInstance = new Timing(); //the second error
}
return _singleInstance;
}
这段代码中有两个我无法弄清楚的错误。
方法
GetInstance()
在标头中声明为静态方法。为什么在cpp文件中我必须省略单词static
?它给出错误:“无法声明成员函数 'static Timing* Timing::GetInstance()' 具有静态链接”。正确的写法是:计时 *Timing::GetInstance() { ... }
为什么不能写
_singleInstance = new Timing();
?它给出错误:“未定义对 Timing::_singleInstance 的引用”。我通过在 cpp 文件中将_singleInstance
定义为全局变量解决了这个错误。
I asked a question earlier today about singletons, and I'm having some difficulties understanding some errors I encountered. I have the following code:
Timing.h
class Timing {
public:
static Timing *GetInstance();
private:
Timing();
static Timing *_singleInstance;
};
Timing.cpp
#include "Timing.h"
static Timing *Timing::GetInstance() { //the first error
if (!_singleInstance) {
_singleInstance = new Timing(); //the second error
}
return _singleInstance;
}
There are two errors in this code which I can't figure out.
The method
GetInstance()
is declared in the header as static. Why in the cpp file do I have to omit the wordstatic
? It gives the error: "cannot declare member function ‘static Timing* Timing::GetInstance()’ to have static linkage". The correct way to write it is:Timing *Timing::GetInstance() { ... }
Why can't I write
_singleInstance = new Timing();
? It gives the error: "undefined reference to Timing::_singleInstance". I solved this error by defining_singleInstance
as a global var in the cpp file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
参考问题 2:您需要在 cpp 文件顶部指定静态变量:
Referencing to question 2: You need to specify the static variable at the top of your cpp-file:
static
与类外的static
的含义完全不同。是的,这不是 C++ 最伟大的设计决策,但是,我们必须接受它。我想抱怨来自链接器,这是因为您已经声明了该变量但从未定义它,使其成为未定义的引用。只需在您的
.cpp
文件中添加如下行:static
within a class means something completely different thanstatic
outside of it. Yeah, not the greatest design decision of C++, but, we have to live with it.I imagine the whining comes from the linker, and it's because you have declared that variable but never defined it, making it an undefined references. Just add in your
.cpp
file a line like:是的,您必须省略 .cpp 文件中的静态
您必须为
_singleInstance '保留内存'
某处,例如在 .cpp 文件中写入以下内容:Timing *Timing::_singleInstance = NULL;
(在成员函数的定义之外)
yes, you have to omit the static in the .cpp file
You'll have to 'reserve memory' for
_singleInstance
somewhere, e.g. by writing the following in the .cpp file:Timing *Timing::_singleInstance = NULL;
(outside the definition of the member functions)
在定义中,需要省略 static 关键字。这是因为这是 C++ 的语法。没什么大不了的。
修复错误 1 后,错误 2 将自动修复。
In the definition, you need to omit the static keyword. Its because that's teh syntax of C++. Nothing big.
Once you fix error number 1, error number 2 will be fixed automatically.
1:当用于类声明之外的函数声明/定义时,静态意味着“本地链接”。
本地链接意味着特定函数只能从该特定文件内的代码引用,这对于类中的方法没有多大意义。
2:由于您的类声明可以多次包含,因此静态成员的实际存储应在 cpp 文件中定义:
1: static means "local linkage" when used for a function declaration/definition outside a class-declaration.
Local linkage means that the particular function can only be referenced from code inside this particular file, and that doesn't make much sense with a method in a class.
2: Since your class declaration can be included multiple times, the actual storage for the static member should be defined in the cpp-file: