首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QList/QHash存储抽象元素

QList/QHash存储抽象元素
EN

Stack Overflow用户
提问于 2011-10-15 02:09:15
回答 2查看 2.3K关注 0票数 3

我想存储在继承自一个类的QHash元素中。所以我得到了:

代码语言:javascript
复制
class ImageInterface
{
public:
    ImageInterface();
    ImageInterface(const QString& path);
    virtual QString getName() const = 0;
};

和实现:

代码语言:javascript
复制
class Image : public ImageInterface
{
public:
    Image();
    Image(const QString& path);
    QString getName() const { return name; }

private:
    QString name;
};

然后我想在QHash中使用它

代码语言:javascript
复制
QHash<QString, ImageInterface> *imageMap;
imageMap = new QHash<QString, ImageInterface>();
ImageInterface *im = new Image(var);
imageMap->insert(im->getName(), *im);
imageMap->take(name);

有错误。

代码语言:javascript
复制
    ../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]

我应该如何实现这样的东西呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-15 02:25:53

你必须存储指向抽象类型的指针。否则,它将尝试实例化该类的实例,因为它是抽象的,所以不能实例化。

代码语言:javascript
复制
QHash<QString, ImageInterface*> *imageMap;
票数 6
EN

Stack Overflow用户

发布于 2011-10-15 02:27:22

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7771736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档