2014-02-21 140 views
2
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类作为类的成员。编译器说:无法解析的外部符号为QObject作为成员(Q_DISABLE_COPY宏)

FSystem.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl FContainer::FContainer(void)" ([email protected]@@@@[email protected]) referenced in function "private: __cdecl FSystem::FSystem(void)" ([email protected]@[email protected])

FContainer构造这里

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

} 

而且FSYSTEM构造这里:

FSystem::FSystem() { } 
+4

这不能是你的实际代码,因为它缺少分号。你可以创建一个[SSCCE](http://sscce.org/)吗?无论如何,我不认为这与'Q_DISABLE_COPY'有关。看起来更像是编译器似乎无法在'A'的默认ctor中使用'QObject'的默认ctor(具有defautl参数的单个参数)。但是如果没有看到涉及的所有代码,就很难确定。 – Angew

+0

然后,我正在更新它。 –

+2

我的猜测是你没有[在头文件中实现模板](http://stackoverflow.com/q/495021/1782465) - 'FContainer' ctor,确切地说。 – Angew

回答

3

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

+0

是的。它的工作。它是一个模板类相关吗? –

+1

@CahitBurakKüçüksütcü我不认为这很有效,因为你还没有尝试过使用这些信号。不幸的是,它们不能工作,因为在类中没有Q_OBJECT宏,并且因为它是一个模板类,所以你不能拥有它。 –

+0

不,它没有用,但我知道了。谢谢你的照顾。 –

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