简体   繁体   English

如何在界面中使用 QMap

[英]How to use a QMap in an interface

I'm trying to use a QMap in my class, but I get the error:我试图在我的班级中使用 QMap,但出现错误:

/* 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?如何在类中使用 QMap?

Thank you in advance.先感谢您。

QMap is a template and you can't just type class QMap instead of the header! QMap 是一个模板,您不能只输入class QMap而不是标题! 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:您必须包含 QMap 的标题:

#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. Wohlstand 的回答不准确,您可以转发声明模板类。 With QMap you would do it like this:使用 QMap 你会这样做:

 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? 如何转发声明 C++ 模板类?

When can I use a forward declaration? 我什么时候可以使用前向声明?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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