0

I use QHash for a small program.

CompleterData.h

#include <QMap>
#include <QList>
#include <QHash>
#include <QPair>
#include <QVariant>

class CompleterData
{

public:
  enum class Type
  {
     Header,       
     SecondHeader, 
     Data,         
     LastUsed      
  };

  CompleterData() = default;

  QHash < CompleterData::Type, QList<QPair<QString, QVariant>>> data();
  void setData( QHash < CompleterData::Type, QList<QPair<QString, QVariant>>> &p_data );
  void addData( CompleterData::Type &p_type,  QList<QPair<QString, QVariant>> &p_rowData );

private:
  QHash <CompleterData::Type, QList<QPair<QString, QVariant>>> m_data;
};

CompleterData.cpp

QHash < CompleterData::Type, QList<QPair<QString, QVariant>>> CompleterData::data()
{
  return m_data;
}

void CompleterData::addData( CompleterData::Type &p_type,  QList<QPair<QString, QVariant>> &p_rowData )
{
  m_data.insert( p_type, p_rowData );
}

void CompleterData::setData( QHash < CompleterData::Type, QList<QPair<QString, QVariant>>> &p_data )
{
  m_data = p_data;
}

I get this error by compiling enter image description here

Where do I have error in this case. I know this kind of error is posted here so many times, but each case has it own reason and even for this simple case I still can not find the reason why? I use VS 2017.

gnase
  • 580
  • 2
  • 10
  • 25
  • 1
    `CompleterData::Type` is a scoped enum, and there is no implicit conversion to integer for it. The same is true for non scoped enum types. `qHash` doesn't know how to calculate hash of your key type. – vahancho Mar 08 '19 at 08:13
  • @vahancho: how should I change in this case? – gnase Mar 08 '19 at 08:14
  • 1
    Two choices: 1) Declare `Type` enum as non scoped, i.e. `enum Type`, 2) Declare your hash table as `QHash >>` and explicitly convert `CompleterData::Type` to int when you insert a pair into the container, i.e. `m_data.insert(static_cast(p_type), p_rowData );`. The is a room for improvements in your code, but it's another topic. – vahancho Mar 08 '19 at 08:20

0 Answers0