gpt4 book ai didi

qt - QList/QHash 存储抽象元素

转载 作者:行者123 更新时间:2023-12-04 11:17:13 29 4
gpt4 key购买 nike

我想存储从一个类继承的 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]

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

最佳答案

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

QHash<QString, ImageInterface*> *imageMap;

关于qt - QList/QHash 存储抽象元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771736/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com