繁体   English   中英

C++ 模板 class 与 static 成员 - 未定义的引用,而我的 ZA81259CEF8E959C3297DF1D456 成员似乎已声明

[英]C++ template class with static member - Undefined reference, whereas my static member seems declared and initialized

我在stackoverflow和其他网站上阅读了很多关于模板类的主题,但我所理解和尝试的所有内容都不起作用,所以请让我公开我的代码,如果你能告诉我我做错了什么,那可能会很好.

注意:我知道使用 Singleton 的不良做法,但这不是谈话,请假设这只是使用模板类的学术练习。


图书馆项目:

// fsingleton.h

#include <QObject>

#if defined(LIB_LIBRARY)
#  define FCORE_EXPORT Q_DECL_EXPORT
#else
#  define FCORE_EXPORT Q_DECL_IMPORT
#endif

template<typename T>
class FCORE_EXPORT FSingleton
{
public:
    static T *instance();
    static void kill();

protected:
    static T *m_instance;
    FSingleton();
    virtual ~FSingleton();

private:
    Q_DISABLE_COPY(FSingleton)
};

template<typename T>
T *FSingleton<T>::m_instance = Q_NULLPTR;

template<typename T>
T *FSingleton<T>::instance()
{
    if (m_instance == Q_NULLPTR) {
        m_instance = new T();
    }
    return m_instance;
}

template<typename T>
void FSingleton<T>::kill()
{
    delete m_instance;
    m_instance = Q_NULLPTR;
}

template<typename T>
FSingleton<T>::FSingleton() {}

template<typename T>
FSingleton<T>::~FSingleton() {}

这里我在一个库项目中,所以构建时定义了LIB_LIBRARY ,所以我们有Q_DECL_EXPORT ,并且库构建成功。


可执行项目:

// testsingleton.h

#include "fsingleton.h"

class TestSingleton : public FSingleton<TestSingleton>
{
public:
    TestSingleton();
    virtual ~TestSingleton();
};

// testsingleton.cpp

#include "testsingleton.h"

TestSingleton::TestSingleton() {}

TestSingleton::~TestSingleton() {}

// tst_utfsingleton.cpp - it is a unit test source file following Qt Test template, I write it as a simple main.cpp function to simplify the example

#include "testsingleton.h"

void main()
{
    TestSingleton* ptr1 = TestSingleton::instance();
}

此可执行文件在链接时无法编译,并给出以下 output:

../debug/obj/tst_utfsingleton.o: In function `FSingleton<TestSingleton>::instance()':
../lib/fsingleton.h:63: undefined reference to `__imp__ZN10FSingletonI13TestSingletonE10m_instanceE'
../lib/fsingleton.h:64: undefined reference to `__imp__ZN10FSingletonI13TestSingletonE10m_instanceE'
../lib/fsingleton.h:68: undefined reference to `__imp__ZN10FSingletonI13TestSingletonE10m_instanceE'
../debug/obj/testsingleton.o: In function `FSingleton<TestSingleton>::FSingleton()':
../lib/fsingleton.h:79: undefined reference to `__imp__ZTV10FSingletonI13TestSingletonE'
../debug/obj/testsingleton.o: In function `FSingleton<TestSingleton>::~FSingleton()':
../lib/fsingleton.h:88: undefined reference to `__imp__ZTV10FSingletonI13TestSingletonE'
../debug/obj/testsingleton.o: In function `FSingleton<TestSingleton>::~FSingleton()':
../lib/fsingleton.h:88: undefined reference to `__imp__ZTV10FSingletonI13TestSingletonE'
collect2.exe: error: ld returned 1 exit status

我不太明白为什么所有这些未定义的引用。 m_instance成员在FSingleton class 中声明,并在 fsingleton.h 中的fsingleton.h定义之后进行初始化。 我看不出我做错了什么,你能帮帮我吗?

问候

好的,由于@drescherjm 的回答,问题得到了解决。

我的理解是模板类是在编译时生成的,具体取决于所需的实例化。 因此,在我的情况下,我在构建库时将模板 class FSingleton标记为__declspec(dllexport) ,但我的库中没有实例化,因此没有导出任何内容。 然后,编译可执行文件,class 被标记为__declspec(dllimport)但因此没有要导入的内容,因为在库构建期间没有构建任何内容,因为实例化FSingleton<TestSIngleton>已在可执行源代码中完成。

如果我说错了,请不要犹豫,纠正我的话^^

再次感谢@drescherjm

暂无
暂无

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

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