QList/QHash存储抽象元素

发布于 2024-12-09 22:02:04 字数 4160 浏览 0 评论 0原文

我想存储在从一个类继承的 QHash 元素中。所以我有:

class ImageInterface
{
public:
    ImageInterface();
    ImageInterface(const QString& path);
    virtual QString getName() const = 0;
};

和实现:

class Image : public ImageInterface
{
public:
    Image();
    Image(const QString& path);
    QString getName() const { return name; }

private:
    QString name;
};

然后我想在 QHash 中使用它:

QHash<QString, ImageInterface> *imageMap;
imageMap = new QHash<QString, ImageInterface>();
ImageInterface *im = new Image(var);
imageMap->insert(im->getName(), *im);
imageMap->take(name);

有错误。

    ../EmulatorKamery/mainwindow.cpp:59:28: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   because the following virtual functions are pure within ‘ImageInterface’:
    ../EmulatorKamery/imageinterface.h:13:25: note:     virtual QString ImageInterface::getName() const
    In file included from /usr/include/qt4/QtCore/qvariant.h:50:0,
                     from /usr/include/qt4/QtCore/qabstractitemmodel.h:45,
                     from /usr/include/qt4/QtGui/qabstractitemview.h:46,
                     from /usr/include/qt4/QtGui/qlistview.h:45,
                     from /usr/include/qt4/QtGui/qlistwidget.h:45,
                     from /usr/include/qt4/QtGui/QListWidget:1,
                     from ../EmulatorKamery/mainwindow.h:5,
                     from ../EmulatorKamery/mainwindow.cpp:1:
    /usr/include/qt4/QtCore/qhash.h: At global scope:
    /usr/include/qt4/QtCore/qhash.h: In instantiation of ‘QHashNode<QString, ImageInterface>’:
    /usr/include/qt4/QtCore/qhash.h:765:9:   instantiated from ‘QHash<K, V>::iterator QHash<K, V>::insert(const Key&, const T&) [with Key = QString, T = ImageInterface]’
    ../EmulatorKamery/mainwindow.cpp:46:48:   instantiated from here
    /usr/include/qt4/QtCore/qhash.h:221:7: error: cannot declare field ‘QHashNode<QString, ImageInterface>::value’ to be of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h: In member function ‘T QHash<K, V>::take(const Key&) [with Key = QString, T = ImageInterface]’:
    ../EmulatorKamery/mainwindow.cpp:59:28:   instantiated from here
    /usr/include/qt4/QtCore/qhash.h:805:24: error: invalid abstract return type for member function ‘T QHash<K, V>::take(const Key&) [with Key = QString, T = ImageInterface]’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    ../EmulatorKamery/mainwindow.cpp:59:28:   instantiated from here
    /usr/include/qt4/QtCore/qhash.h:808:18: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h:813:24: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h:813:11: error: cannot declare variable ‘t’ to be of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    ../EmulatorKamery/mainwindow.cpp:59:28:   instantiated from here
    /usr/include/qt4/QtCore/qhash.h:819:16: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h:821:14: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h:822:1: warning: control reaches end of non-void function [-Wreturn-type]

我应该如何实现这样的事情?

I'd like to store in QHash elements that inherits from one class. So I've got:

class ImageInterface
{
public:
    ImageInterface();
    ImageInterface(const QString& path);
    virtual QString getName() const = 0;
};

And implementation:

class Image : public ImageInterface
{
public:
    Image();
    Image(const QString& path);
    QString getName() const { return name; }

private:
    QString name;
};

Then I want to use it in QHash:

QHash<QString, ImageInterface> *imageMap;
imageMap = new QHash<QString, ImageInterface>();
ImageInterface *im = new Image(var);
imageMap->insert(im->getName(), *im);
imageMap->take(name);

There are errors.

    ../EmulatorKamery/mainwindow.cpp:59:28: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   because the following virtual functions are pure within ‘ImageInterface’:
    ../EmulatorKamery/imageinterface.h:13:25: note:     virtual QString ImageInterface::getName() const
    In file included from /usr/include/qt4/QtCore/qvariant.h:50:0,
                     from /usr/include/qt4/QtCore/qabstractitemmodel.h:45,
                     from /usr/include/qt4/QtGui/qabstractitemview.h:46,
                     from /usr/include/qt4/QtGui/qlistview.h:45,
                     from /usr/include/qt4/QtGui/qlistwidget.h:45,
                     from /usr/include/qt4/QtGui/QListWidget:1,
                     from ../EmulatorKamery/mainwindow.h:5,
                     from ../EmulatorKamery/mainwindow.cpp:1:
    /usr/include/qt4/QtCore/qhash.h: At global scope:
    /usr/include/qt4/QtCore/qhash.h: In instantiation of ‘QHashNode<QString, ImageInterface>’:
    /usr/include/qt4/QtCore/qhash.h:765:9:   instantiated from ‘QHash<K, V>::iterator QHash<K, V>::insert(const Key&, const T&) [with Key = QString, T = ImageInterface]’
    ../EmulatorKamery/mainwindow.cpp:46:48:   instantiated from here
    /usr/include/qt4/QtCore/qhash.h:221:7: error: cannot declare field ‘QHashNode<QString, ImageInterface>::value’ to be of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h: In member function ‘T QHash<K, V>::take(const Key&) [with Key = QString, T = ImageInterface]’:
    ../EmulatorKamery/mainwindow.cpp:59:28:   instantiated from here
    /usr/include/qt4/QtCore/qhash.h:805:24: error: invalid abstract return type for member function ‘T QHash<K, V>::take(const Key&) [with Key = QString, T = ImageInterface]’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    ../EmulatorKamery/mainwindow.cpp:59:28:   instantiated from here
    /usr/include/qt4/QtCore/qhash.h:808:18: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h:813:24: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h:813:11: error: cannot declare variable ‘t’ to be of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    ../EmulatorKamery/mainwindow.cpp:59:28:   instantiated from here
    /usr/include/qt4/QtCore/qhash.h:819:16: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h:821:14: error: cannot allocate an object of abstract type ‘ImageInterface’
    ../EmulatorKamery/imageinterface.h:8:11: note:   since type ‘ImageInterface’ has pure virtual functions
    /usr/include/qt4/QtCore/qhash.h:822:1: warning: control reaches end of non-void function [-Wreturn-type]

How should I implemented something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2024-12-16 22:02:04

您必须存储指向抽象类型的指针。否则它将尝试实例化该类的实例,但由于它是抽象的,因此无法实例化。

QHash<QString, ImageInterface*> *imageMap;

You have to store pointers to your abstract type. Otherwise it's going to try to instantiate instances of that class, which it can't since it's abstract.

QHash<QString, ImageInterface*> *imageMap;
以歌曲疗慰 2024-12-16 22:02:04

您必须在哈希容器中存储指针(或更好的智能指针,如 std::shared_ptr 或 QSharedPointer)。

You have to store pointers (or better smart pointers like std::shared_ptr or QSharedPointer) in your hash container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文