C++ (Qt)#include <QtCore/QCoreApplication>#include <QtCore/QHash>#include <QtCore/QLatin1String> #include "Types.h" int main(int argc, char** argv){ QCoreApplication a(argc,argv); // QHash with custom type of key - 'SensorCode'. // Reimplement of the function 'qHash(SensorCode)' - in 'Types.h'. QHash<SensorCode,QLatin1String> storage; // !!! Instantiating by my custom kye type - 'SensorCode'. // Work with storage. SensorCode code(); storage[code] = QLatin1String("My sensor 1"); return a.exec();}
C++ (Qt)#ifndef TYPES_H#define TYPES_H #include <QtCore/QHash> // Sensor code - bytes 'CD', 'B1B0' in formular - sensor code relatevly the controller. struct SensorCode { quint8 cd; quint16 b1b0; // Null val. SensorCode() { this->cd=0; this->b1b0=0; } // Override of '=' operator. void operator= ( const SensorCode& other ) { this->cd = other.cd; this->b1b0 = other.b1b0; } }; // Override of 'operator=='.inline bool operator== (const SensorCode& code1, const SensorCode& code2); // Override of 'operator!='.inline bool operator!= (const SensorCode& code1, const SensorCode& code2); // Override of 'SensorCode' hashing.inline uint qHash (const SensorCode &code, uint seed = 0); #endif
C++ (Qt)#include "Types.h" // Override of 'operator=='.bool operator== (const SensorCode& code1, const SensorCode& code2){ if ( code1.cd != code2.cd ) return false; if ( code1.b1b0 != code2.b1b0 ) return false; return true;} // Override of 'operator!='.bool operator!= (const SensorCode& code1, const SensorCode& code2){ return ! ( code1 == code2 );} // Override of 'SensorCode' hashing.inline uint qHash (const SensorCode &code, uint seed){ uint key = 0x00; key = ((uint)(code.cd))*0x10000; key += (uint)(code.b1b0); return qHash ( key, seed );}
QHash<SensorCode,QLatin1String> storage; // !!! Instantiating by my custom kye type - 'SensorCode'.
16:04:28: Выполняются этапы для проекта QHash...16:04:28: Настройки не изменились, этап qmake пропускается.16:04:28: Запускается: «C:\DevTools\Qt_4.8.6\MinGW-4.8.2-i686-posix-dwarf-rev3\mingw32\bin\mingw32-make.exe» C:/DevTools/Qt_4.8.6/MinGW-4.8.2-i686-posix-dwarf-rev3/mingw32/bin/mingw32-make -f Makefile.Releasemingw32-make[1]: Entering directory 'D:/Projects/QtProjects/build-QHash-Qt_5_6-Release'g++ -c -pipe -fno-keep-inline-dllexport -O2 -std=gnu++0x -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_NO_DEBUG -DQT_CORE_LIB -I..\QHash -I. -IC:\DevTools\Qt\5.6\mingw49_32\include -IC:\DevTools\Qt\5.6\mingw49_32\include\QtCore -Ibuild -IC:\DevTools\Qt\5.6\mingw49_32\mkspecs\win32-g++ -o build\main.o ..\QHash\main.cc..\QHash\main.cc: In function 'int main(int, char**)':..\QHash\main.cc:17:12: error: no match for 'operator[]' (operand types are 'QHash<SensorCode, QLatin1String>' and 'SensorCode()') storage[code] = QLatin1String("My sensor 1"); ^..\QHash\main.cc:17:12: note: candidates are:In file included from C:\DevTools\Qt\5.6\mingw49_32\include/QtCore/QHash:1:0, from ..\QHash\main.cc:2:C:\DevTools\Qt\5.6\mingw49_32\include/QtCore/qhash.h:721:22: note: T& QHash<Key, T>::operator[](const Key&) [with Key = SensorCode; T = QLatin1String] Q_INLINE_TEMPLATE T &QHash<Key, T>::operator[](const Key &akey) ^C:\DevTools\Qt\5.6\mingw49_32\include/QtCore/qhash.h:721:22: note: no known conversion for argument 1 from 'SensorCode()' to 'const SensorCode&'C:\DevTools\Qt\5.6\mingw49_32\include/QtCore/qhash.h:715:27: note: const T QHash<Key, T>::operator[](const Key&) const [with Key = SensorCode; T = QLatin1String] Q_INLINE_TEMPLATE const T QHash<Key, T>::operator[](const Key &akey) const ^C:\DevTools\Qt\5.6\mingw49_32\include/QtCore/qhash.h:715:27: note: no known conversion for argument 1 from 'SensorCode()' to 'const SensorCode&'Makefile.Release:220: recipe for target 'build/main.o' failedmingw32-make[1]: *** [build/main.o] Error 1mingw32-make[1]: Leaving directory 'D:/Projects/QtProjects/build-QHash-Qt_5_6-Release'Makefile:34: recipe for target 'release' failedmingw32-make: *** [release] Error 216:04:30: Процесс «C:\DevTools\Qt_4.8.6\MinGW-4.8.2-i686-posix-dwarf-rev3\mingw32\bin\mingw32-make.exe» завершился с кодом 2.Ошибка при сборке/установке проекта QHash (комплект: Qt 5.6)Во время выполнения этапа «Сборка»16:04:30: Прошло времени: 00:01.
D:\Projects\QtProjects\QHash\main.cc:-1: ошибка: undefined reference to `qHash(SensorCode const&, unsigned int)'D:\Projects\QtProjects\QHash\main.cc:-1: ошибка: undefined reference to `qHash(SensorCode const&, unsigned int)':-1: ошибка: build/main.o: bad reloc address 0x120 in section `.text$_ZN5QHashI10SensorCode13QLatin1StringEixERKS0_[__ZN5QHashI10SensorCode13QLatin1StringEixERKS0_]'collect2.exe:-1: ошибка: error: ld returned 1 exit status
C++ (Qt)#include <QtCore/QCoreApplication>#include <QtCore/QHash>#include <QtCore/QLatin1String> #include "Types.h" int main(int argc, char** argv){ QCoreApplication a(argc,argv); QHash<SensorCode,QLatin1String> storage; SensorCode code; code.cd = 0; code.b1b0 = 0; storage[code] = QLatin1String("My sensor 1"); return a.exec();}
C++ (Qt)#ifndef TYPES_H#define TYPES_H #include <QtCore/QHash> //namespace SensorAttributes//{ // Sensor code - bytes 'CD', 'B1B0' in formular - sensor code relatevly the controller. struct SensorCode { quint8 cd; quint16 b1b0; // Null val. SensorCode() { this->cd=0; this->b1b0=0; } // Override of '=' operator. void operator= ( const SensorCode& other ) { this->cd = other.cd; this->b1b0 = other.b1b0; } };//} // namespace SensorAttributes // Override of 'operator=='.bool operator== (const SensorCode& code1, const SensorCode& code2); // Override of 'operator!='.bool operator!= (const SensorCode& code1, const SensorCode& code2); // Override of 'SensorCode' hashing.uint qHash (const SensorCode &code, uint seed = 0); #endif
C++ (Qt)#include "Types.h" // Override of 'operator=='.bool operator== (const SensorCode& code1, const SensorCode& code2){ if ( code1.cd != code2.cd ) return false; if ( code1.b1b0 != code2.b1b0 ) return false; return true;} // Override of 'operator!='.bool operator!= (const SensorCode& code1, const SensorCode& code2){ return ! ( code1 == code2 );} // Override of 'SensorCode' hashing.uint qHash (const SensorCode &code, uint seed){ uint key = 0x00; key = ((uint)(code.cd))*0x10000; key += (uint)(code.b1b0); return qHash ( key, seed );}
namespace SensorAttributes{ ... struct SensorCode { ... } ...}
QHash<SensorAttributes::SensorCode,QLatin1String> storage;
no matching function for call to 'qHash(const SensorAttributes::SensorCode&)' Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(t))) ^
C++ (Qt)#include <QtCore/QCoreApplication>#include <QtCore/QHash>#include <QtCore/QLatin1String>#include <QtDebug> #include "Types.h" int main(int argc, char** argv){ QCoreApplication a(argc,argv); // QHash с особым типом ключа - 'SensorCode'. // Перегрузка qHash(SensorCode) - в 'Types.h'. QHash<SensorAttributes::SensorCode,QLatin1String> storage; // Работа с хранилищем. SensorAttributes::SensorCode code1(0xaa,0xbbcc), code2(0x01,0x0203); storage[code1] = QLatin1String("My sensor 1"); storage[code2] = QLatin1String("My sensor 2"); qInfo() << "Key1:" << storage.value(code1,QLatin1String("none")); qInfo() << "Key2:" << storage.value(code2,QLatin1String("none")); QMetaObject::invokeMethod(&a,"quit",Qt::QueuedConnection); return a.exec();}
C++ (Qt)#ifndef TYPES_H#define TYPES_H #include <QtCore/QHash> namespace SensorAttributes{ // Sensor code - bytes 'CD', 'B1B0' in formular - sensor code relatevly the controller. struct SensorCode { quint8 cd; quint16 b1b0; // Null val. SensorCode() { this->cd=0; this->b1b0=0; } SensorCode(quint8 arg1, quint16 arg2) { this->cd=arg1; this->b1b0=arg2; } // Override of '=' operator. void operator= ( const SensorCode& other ) { this->cd = other.cd; this->b1b0 = other.b1b0; } };} // namespace SensorAttributes // Override of 'operator=='.bool operator== (const SensorAttributes::SensorCode& code1, const SensorAttributes::SensorCode& code2); // Override of 'operator!='.bool operator!= (const SensorAttributes::SensorCode& code1, const SensorAttributes::SensorCode& code2); // Override of 'SensorCode' hashing.uint qHash (const SensorAttributes::SensorCode &code, uint seed = 0); #endif
C++ (Qt)#include "Types.h" // Override of 'operator=='.bool operator== (const SensorAttributes::SensorCode& code1, const SensorAttributes::SensorCode& code2){ if ( code1.cd != code2.cd ) return false; if ( code1.b1b0 != code2.b1b0 ) return false; return true;} // Override of 'operator!='.bool operator!= (const SensorAttributes::SensorCode& code1, const SensorAttributes::SensorCode& code2){ return ! ( code1 == code2 );} // Override of 'SensorCode' hashing.uint qHash (const SensorAttributes::SensorCode &code, uint seed){ uint key = 0x00; key = ((uint)(code.cd))*0x10000; key += (uint)(code.b1b0); return qHash ( key, seed ) ^ seed;}
C++ (Qt)#include <QtCore/QCoreApplication>#include <QtCore/QHash>#include <QtCore/QLatin1String>#include <QtDebug> #include "Types.h" int main(int argc, char** argv){ QCoreApplication a(argc,argv); // QHash с особым типом ключа - 'SensorCode'. // Перегрузка qHash(SensorCode) - в 'Types.h'. QHash<SensorAttributes::SensorCode,QLatin1String> storage; // Работа с хранилищем. SensorAttributes::SensorCode code1(0xaa,0xbbcc), code2(0x01,0x0203); storage[code1] = QLatin1String("My sensor 1"); storage[code2] = QLatin1String("My sensor 2"); qInfo() << "Key1:" << storage.value(code1,QLatin1String("none")); qInfo() << "Key2:" << storage.value(code2,QLatin1String("none")); QMetaObject::invokeMethod(&a,"quit",Qt::QueuedConnection); return a.exec();}
C++ (Qt)#ifndef TYPES_H#define TYPES_H #include <QtCore/QHash> namespace SensorAttributes{ // Sensor code - bytes 'CD', 'B1B0' in formular - sensor code relatevly the controller. struct SensorCode { quint8 cd; quint16 b1b0; // Null val. SensorCode() { this->cd=0; this->b1b0=0; } SensorCode(quint8 arg1, quint16 arg2) { this->cd=arg1; this->b1b0=arg2; } // Override of '=' operator. void operator= ( const SensorCode& other ) { this->cd = other.cd; this->b1b0 = other.b1b0; } // Overrride of 'operator=='. bool operator== ( const SensorCode& code ) const { if ( this->cd != code.cd ) return false; if ( this->b1b0 != code.b1b0 ) return false; return true; } }; // Override of 'SensorCode' hashing. inline uint qHash ( const SensorCode &code, uint seed = 0 ) { uint key = 0x00; key = ((uint)(code.cd))*0x10000; key += (uint)(code.b1b0); return ::qHash ( key, seed ); } } // namespace SensorAttributes #endif