未定义的引用 - C ++链接器错误 [英] Undefined reference - C++ linker error

查看:148
本文介绍了未定义的引用 - C ++链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个未定义的引用错误消息,在此语句:

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


推荐答案

单例类?也就是说。您只需要该类的一个实例,并且您希望该实例可供包含该类的任何人使用。我认为它通常被称为Singleton,下面的例子工作原理:

Are you trying to implement a Singleton class? 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.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:

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

请注意,构造函数是私有的,我们不希望任何人创建我们的单例的实例,除非它通过'instance()'运算符。

Note that the constructor is private, we don't want anyone to be creating instances of our singleton, unless its via the 'instance()' operator.

这篇关于未定义的引用 - C ++链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆