QObject作为成员的未解析外部符号(Q_DISABLE_COPY宏)

时间:2014-02-21 16:00:07

标签: c++ qt qobject

template <class T>
class FContainer : public QObject
{

public:

    FContainer();

    inline void append(const T &t);
    inline void clear();
    inline void remove(const T &t);

    inline const T& at(int index) const { return m_list[index]; }
    inline const T& first() const { return m_list.first(); }
    inline const T& last() const { return m_list.last(); }

    inline int indexOf(const T &t){ return m_list.indexOf(t); }
    inline int size() { return m_list.size(); }

signals:

    void appended(const T &t);
    void cleared();
    void removed(const T &t);
    void updated();


private:

    QList<T> m_list;
};

class FSystem
{
public:

    inline const FContainer<FMaterial>& materials() const { return m_materials; }
    inline const FContainer<FObject>& objects() const { return m_objects; }

    inline FContainer<FMaterial>& materials() { return m_materials; }
    inline FContainer<FObject>& objects() { return m_objects; }

    static FSystem* Instance() { return m_instance; }

private:
    FSystem();

    FContainer<FMaterial> m_materials;
    FContainer<FObject> m_objects;

    static FSystem *m_instance;
};

我有一个关于使用QObject类作为类成员的探测器。编译说:

  

FSystem.obj:-1:错误:LNK2019:未解析的外部符号“public:__ cdecl FContainer :: FContainer(void)”(?? 0?$ FContainer @ VFMaterial @@@@ QEAA @ XZ)在函数中引用“ private:__ cdecl FSystem :: FSystem(void)“(?? 0FSystem @@ AEAA @ XZ)

FContainer Constructor Here

template <class T>
FContainer<T>::FContainer()
    : QObject()
{

}

和FSystem Constructor在这里:

FSystem::FSystem() { }

2 个答案:

答案 0 :(得分:3)

几乎可以肯定的是,您在源文件中而不是在标头中定义FContainer::FContainer();。由于它位于模板类中,因此编译器需要能够在实例化时查看正文,或者进行显式实例化。

答案 1 :(得分:1)

可以使用模板化对象,但由于它不具有Q_OBJECT宏,因此它也不能拥有自己的信号。如果您尝试使用代码,则会发现无法连接到对象的信号。

您需要将包含信号的QObject与模板类分开。因此,您的信号不能依赖于模板参数:您必须强制发出变体而不是T。这正是QFutureWatcher

实施过程中采用的方法
class FContainerBase : public QObject {
    Q_OBJECT
public:
    FContainerBase(QObject*parent = 0);
    Q_SIGNAL void appended(const QVariant &);
    Q_SIGNAL void cleared();
    Q_SIGNAL void removed(const QVariant &);
    Q_SIGNAL void updated();
};

template <class T>
class FContainer : public FContainerBase
{
    QList<T> m_list;
public:
    FContainer() { // implementation in the header! }

    inline void append(const T &t);
    inline void clear();
    inline void remove(const T &t);

    inline const T& at(int index) const { return m_list[index]; }
    inline const T& first() const { return m_list.first(); }
    inline const T& last() const { return m_list.last(); }

    inline int indexOf(const T &t){ return m_list.indexOf(t); }
    inline int size() { return m_list.size(); }
};