Issue with QHash
I been trying and trying to get this to work but it just refuses to work. I read the QT documentation and I'm just unable to get the insert function to f开发者_运维技巧unction. When I build I get the following complication errors
/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
here is the header file:
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());
}
and the implementation
SDStreamEmbed::SDStreamEmbed() {
}
SDStreamEmbed::SDStreamEmbed(const SDStreamEmbed& other) {
}
QString SDStreamEmbed::FriendlyName() const {
return friendlyName;
}
and how I am invoking it
SDChatEmbed embedTest();
ChatEmbeds.insert("DemoTest", embedTest);
and the definition of ChatEmbeds
QHash<QString, SDStreamEmbed> StreamEmbeds;
Replace:
SDChatEmbed embedTest();
with:
SDChatEmbed embedTest;
The compiler interprets the first line as a function declaration. This is visible in the error message: it deduces the following type for the second argument:
SDChatEmbed (&)()
and that's a function signature.
I don't think you need an explicit QString
cast/construction for the first argument since QString
has a constructor that takes a const char*
, so that one should get converted automatically.
(See here for some interesting info.)
精彩评论