c++qtqmap

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.


Solution

  • 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)