简体   繁体   中英

How to use a QMap in an interface

I'm trying to use a QMap in my class, but I get the error:

/* path */.h:18: error: template argument required for ‘class QMap’
 class QMap;
       ^~~~

The implementation

class QMap;

class MappingInterface
{

public:
    virtual ~MappingInterface() {}

    virtual QMap<QString, QString> itemsMap() const = 0;
};

#define MappingInterface_iid "com.myapp.MappingInterface"

    Q_DECLARE_INTERFACE(MappingInterface, MappingInterface_iid)

How can I use a QMap inside a class?

Thank you in advance.

QMap is a template and you can't just type class QMap instead of the header! The short class prototype can be used for pointers only, for objects and the references you must include the header of the full class declaration!

You must include QMap's header:

#include <QMap>
#include <QString>

class MappingInterface
{
public:
    virtual ~MappingInterface() {}
    virtual QMap<QString, QString> itemsMap() const = 0;
};

#define MappingInterface_iid "com.myapp.MappingInterface"

Q_DECLARE_INTERFACE(MappingInterface, MappingInterface_iid)

Wohlstand's answer is not accurate, you can forward declare template classes. With QMap you would do it like this:

 template <class Key, class T> class QMap;

you can indent it a bit if you prefer of course

template <class Key, class T> 
class QMap;

check these answers for more info about this

How to forward declare a C++ template class?

When can I use a forward declaration?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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