问题与QHash

时间:2011-10-06 09:49:25

标签: c++ qt

我一直在尝试让它发挥作用,但它只是拒绝工作。我阅读了QT文档,但我无法使插入函数正常运行。当我构建时,我得到以下复杂错误

/home/mmanley/projects/StreamDesk/libstreamdesk/SDDatabase.cpp: In constructor 'SDDatabase::SDDatabase()':
/home/mmanley/projects/StreamDesk/libstreamdesk/SDDatabase.cpp:27:44: error: no matching function for call to 'QHash<QString, SDChatEmbed>::insert(const char [9], SDChatEmbed (&)())'
/usr/include/qt4/QtCore/qhash.h:751:52: note: candidate is: QHash<Key, T>::iterator         QHash<Key, T>::insert(const Key&, const T&) [with Key = QString, T = SDChatEmbed]
make[2]: *** [libstreamdesk/CMakeFiles/streamdesk.dir/SDDatabase.cpp.o] Error 1
make[1]: *** [libstreamdesk/CMakeFiles/streamdesk.dir/all] Error 2

这是头文件:

class SDStreamEmbed {
        Q_OBJECT
    public:
        SDStreamEmbed();
        SDStreamEmbed(const SDStreamEmbed &other);

        QString FriendlyName() const;

        SDStreamEmbed &operator=(const SDStreamEmbed &other) {return *this;}
        bool operator==(const SDStreamEmbed &other) const {return friendlyName == other.friendlyName;}

    private:
        QString friendlyName;
};

Q_DECLARE_METATYPE(SDStreamEmbed)

inline uint qHash(const SDStreamEmbed &key) {
    return qHash(key.FriendlyName());
}

和实施

SDStreamEmbed::SDStreamEmbed() {

}

SDStreamEmbed::SDStreamEmbed(const SDStreamEmbed& other) {

}

QString SDStreamEmbed::FriendlyName() const {
    return friendlyName;
}

以及我如何调用它

SDChatEmbed embedTest();
ChatEmbeds.insert("DemoTest", embedTest);

和ChatEmbeds的定义

QHash<QString, SDStreamEmbed> StreamEmbeds;

1 个答案:

答案 0 :(得分:3)

替换:

SDChatEmbed embedTest();

使用:

SDChatEmbed embedTest;

编译器将第一行解释为函数声明。这在错误消息中可见:它为第二个参数推导出以下类型:

SDChatEmbed (&)()

这是一个功能签名。

我认为你不需要第一个参数的显式QString强制转换/构造,因为QString有一个带const char*的构造函数,因此应该自动转换。

(有关一些有趣的信息,请参阅here。)