由于使用静态变量而导致未定义的引用错误

发布于 2024-09-17 22:23:46 字数 909 浏览 4 评论 0原文

今天早些时候我问了一个关于单例的问题,我在理解我遇到的一些错误时遇到了一些困难。我有以下代码:

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;
}

这段代码中有两个我无法弄清楚的错误。

  1. 方法 GetInstance() 在标头中声明为静态方法。为什么在cpp文件中我必须省略单词static?它给出错误:“无法声明成员函数 'static Timing* Timing::GetInstance()' 具有静态链接”。正确的写法是:

    计时 *Timing::GetInstance() { ... }  
    
  2. 为什么不能写_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.

  1. The method GetInstance() is declared in the header as static. Why in the cpp file do I have to omit the word static? 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() { ... }  
    
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断念 2024-09-24 22:23:47

参考问题 2:您需要在 cpp 文件顶部指定静态变量:

Timing* Timing::_singleInstance = NULL;

Referencing to question 2: You need to specify the static variable at the top of your cpp-file:

Timing* Timing::_singleInstance = NULL;
南街九尾狐 2024-09-24 22:23:47
  1. 类内的

    static 与类外的 static 的含义完全不同。是的,这不是 C++ 最伟大的设计决策,但是,我们必须接受它。

  2. 我想抱怨来自链接器,这是因为您已经声明了该变量但从未定义它,使其成为未定义的引用。只需在您的 .cpp 文件中添加如下行:

    计时* 计时::_singleInstance;
    
  1. static within a class means something completely different than static outside of it. Yeah, not the greatest design decision of C++, but, we have to live with it.

  2. 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:

    Timing* Timing::_singleInstance;
    
不气馁 2024-09-24 22:23:47
  1. 是的,您必须省略 .cpp 文件中的静态

  2. 您必须为 _singleInstance '保留内存' 某处,例如在 .cpp 文件中写入以下内容:

    Timing *Timing::_singleInstance = NULL;

(在成员函数的定义之外)

  1. yes, you have to omit the static in the .cpp file

  2. 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)

你的往事 2024-09-24 22:23:47

在定义中,需要省略 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.

风追烟花雨 2024-09-24 22:23:46

1:当用于类声明之外的函数声明/定义时,静态意味着“本地链接”。

本地链接意味着特定函数只能从该特定文件内的代码引用,这对于类中的方法没有多大意义。

2:由于您的类声明可以多次包含,因此静态成员的实际存储应在 cpp 文件中定义:

#include "Timing.h"

Timing* Timing::_singleInstance;

Timing *Timing::GetInstance() {  //the first error
    if (!_singleInstance) {
        _singleInstance = new Timing();  //the second error
    }
    return _singleInstance;
}

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:

#include "Timing.h"

Timing* Timing::_singleInstance;

Timing *Timing::GetInstance() {  //the first error
    if (!_singleInstance) {
        _singleInstance = new Timing();  //the second error
    }
    return _singleInstance;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文