0
votes

I have a couple of classes:

A.h
class Abase_obj : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QAction* playAction READ playAction CONSTANT)
public:
    Abase_obj(QObject* parent = nullptr) : QObject(parent) {}
    virtual QAction* playAction() = 0;

signals:
    void someSignal()
slots:
    virtual void baseSlot() = 0;
};

template <class T>
class Abase : public Abase_obj
{
public:
    Abase(QObject* parent = nullptr) : Abase_obj(parent) {...}
    void baseSlot() override;
    QAction* playAction() override;
protected:
    QWeakPointer<T> m_currentItem;
};

template <class T>
Abase<T>::baseSlot() {
   auto item = m_currentItem.lock();
   item->someMethodOfTemplateClass(); // here is the error
}

B_inh.h

#include "A.h"

class MediaItem;
class B_inh : public Abase<MediaItem>
{
    Q_OBJECT
    Q_PROPERTY(MediaItem* getItem READ getItem)
public:
    // constructor
    MediaItem* getItem();
public slot:
    // some new slot

}

So I use Abase_obj as I can't type Q_OBJECT macro in the templated class and define there some slots and signals. I know that I can define there slots which doesn't has templated parametres. But I am using the template method directly in this slot. But as Abase_obj metaobject doesn't know about this class, I get the error: error: C2039: 'someMethodOfTemplateClass': is not a member of 'QSharedPointer' and error: C2027: use of undefined type 'MediaItem'. So I have 2 questions: first one is how should I implement this slot so it could work, and the second question is in the comment above.

Upd: If I make #include "MediaItem.h" instead of forward declaring it, I am getting errors:

LNK2001: unresolved external symbol "public: static struct QMetaObject const Abase_obj::staticMetaObject" : in file B_inh.cpp.obj
LNK2001: unresolved external symbol "public: virtual void * __cdecl Abase_obj::qt_metacast(char const *)" : in file B_inh.cpp.obj
LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Abase_obj::metaObject(void)const " : in file B_inh.cpp.obj

If I remove Q_OBJECT macro and all Q_PROPERTies from Abase_obj and add them to the B_inh class, everything is compiling and working, but I don't want to implement these properties every time in all of the derived classes

1
What if you include header where MediaItem is declared instead of forward declaring it? - vahancho
I am getting some linker errors: ``` LNK2001: unresolved external symbol "public: static struct QMetaObject const Abase_obj::staticMetaObject" LNK2001: unresolved external symbol "public: virtual void * __cdecl Abase_obj::qt_metacast(char const *)" LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Abase_obj::metaObject(void)const " ``` - RomaFUN
Looks like the moc file is not compiled in. Make sure moc is running and generates moc_A.h - chehrlic
@chehrlic how can I check it? - RomaFUN
I tried building your code. I had to fix some typos, and fill in empty implementations of functions, and make up a definition of MediaItem. But it worked fine. I'm not getting any errors. So I think the problem is elsewhere. Does your build process include moc? I used a qmake project that handles that automatically. - JarMan

1 Answers

0
votes

The code is correct. The mistake was in my qbs dependencies. File A.h just haven't been compiled and haven't been correctly included to the B_inh.h, so I had these linker errors