简体   繁体   English

未定义参考-C ++链接器错误

[英]Undefined reference - C++ linker error

I'm getting an Undefined reference error message, on this statement: 我收到以下语句的未定义参考错误消息:

GlobalClass *GlobalClass::s_instance = 0;

Any ideas? 有任何想法吗? Code is shown below: 代码如下所示:

================================================ ================================================

#ifndef GLOBALCLASS_H_
#define GLOBALCLASS_H_

#include <string>
class GlobalClass {

public:

    std::string get_value();

    void set_value(std::string);

    static GlobalClass *instance();

    static GlobalClass *s_instance;

private:

    std::string m_value;
};

#endif /* GLOBALCLASS_H_ */

=============================================== ===============================================

#include <string>
#include "GlobalClass.h"



/*
 GlobalClass(int v = 0)
 {
 m_value = v;
 }
 */

    static GlobalClass *s_instance;

    std::string GlobalClass::get_value()
    {
        return m_value;
    }

    void GlobalClass::set_value(std::string v)
    {
        m_value = v;
    }

    static GlobalClass *instance() {
        if (!s_instance)
            s_instance = new GlobalClass;
        return s_instance;
    }

=========================================================== ================================================== =========

#include <iostream>
#include "GlobalClass.h"

using namespace std;

int main() {

    GlobalClass::s_instance = 0;


    std::string myAddress = "abc";
    GlobalClass::instance()->set_value(myAddress);  \\ <=== compiler error
    std::cout << "====>address is is " << GlobalClass::instance()->get_value()
            << std::endl;
    return 0;
}

Are you trying to implement a Singleton class? 您是否要实现Singleton类? Ie. IE浏览器。 You want only a single instance of of the class, and you want that instance available to anyone who includes the class. 您只需要该类的单个实例,并且希望该实例可供包含该类的任何人使用。 I think its commonly known as a Singleton, the following example works as expected: 我认为它通常称为Singleton,下面的示例可以正常工作:

Singleton.h: Singleton.h:

#include <string>
class Singleton
{
public:
    static Singleton* instance()
    {
        if ( p_theInstance == 0 )
            p_theInstance = new Singleton;
        return p_theInstance;
    }
    void setMember( const std::string& some_string )
    {
        some_member = some_string;
    }
    const std::string& get_member() const
    {
        return some_member;
    }

private:
    Singleton() {}
    static Singleton* p_theInstance;
    std::string some_member;
};

Singleton.cpp: Singleton.cpp:

Singleton* Singleton::p_theInstance = 0;

main.cpp: main.cpp中:

#include <string>
#include <iostream>
#include "Singleton.h"

int main()
{
    std::string some_string = "Singleton class";
    Singleton::instance()->setMember(some_string);
    std::cout << Singleton::instance()->get_member() << "\n";
}

Note that the constructor is private, we don't want anyone to be creating instances of our singleton, unless its via the 'instance()' operator. 请注意,该构造函数是私有的,除非通过'instance()'运算符,否则我们不希望任何人创建我们的单例实例。

When you declare a static field s_instance in your .h file, it only tells the compiler that this field exists somewhere. 在.h文件中声明静态字段s_instance时,它仅告诉编译器该字段存在于某处。 This allows your main function to reference it. 这使您的main功能可以引用它。 However, it doesn't define the field anywhere, ie, no memory is reserved for it, and no initial value is assigned to it. 但是,它没有在任何地方定义该字段,即,没有为它保留任何内存,也没有为其分配初始值。 This is analogous to the difference between a function prototype (usually in a .h file) and function definition (in a .cpp file). 这类似于函数原型(通常在.h文件中)和函数定义(.cpp文件中)之间的区别。

In order to actually define the field, you need to add the following line to your .cpp file at global scope (not inside any function): 为了实际定义该字段,您需要在全局范围内(不在任何函数内)向.cpp文件添加以下行:

GlobalClass* GlobalClass::s_instance = 0;

It is important that you don't add the static modifier to this definition (although you should still have the static modifier on the declaration inside the class in the .h file). 重要的是,不要在此定义中添加static修饰符(尽管.h文件中的类内的声明上仍应具有static修饰符)。 When a definition outside a class is marked static , that definition can only be used inside the same .cpp file. 当一个类之外的定义标记为static ,该定义只能在同一.cpp文件中使用。 The linker will act as if it doesn't exist if it's used in other .cpp files. 如果在其他.cpp文件中使用了链接器,则该链接器的作用就好像它不存在一样。 This meaning of static is different from static inside a class and from static inside a function. 这意思static不同于static类内部,并从static函数内部。 I'm not sure why the language designers used the same keyword for three different things, but that's how it is. 我不确定语言设计师为什么在三个不同的事物上使用相同的关键字,但是事实就是如此。

如果我了解您想要做的只是使用此方法的问题:

GlobalClass::s_instance = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM