C++ 子类化自定义对象的QList

C++ 子类化自定义对象的QList,c++,qt,qlist,C++,Qt,Qlist,我有一个类点定义如下: class Point { public: inline Point() { m_x=0; m_y=0;} protected: int m_x; int m_y; }; Q_DECLARE_METATYPE(Point) 然后我想定义一个自定义的QList点对象,因为我想用一些Point相关的方法扩展基本方法,例如下面的hasX(intx): class PointList : public QList<Point> { publ

我有一个类
定义如下:

class Point
{
public:
    inline Point() { m_x=0; m_y=0;}

protected:
    int m_x;
    int m_y;
};
Q_DECLARE_METATYPE(Point)
然后我想定义一个自定义的
QList
点对象,因为我想用一些
Point
相关的方法扩展基本方法,例如下面的
hasX(intx)

class PointList : public QList<Point>
{
public:
    inline PointList() {;}
    inline PointList(const QList<Point>& points) : QList<PointList>(points){ ; }

    bool hasX(int x) const {
        for (const Point& p: *this)
            if (p.x() == x)
                return true;
        return false;
    }
};
类点列表:公共QList
{
公众:
内联点列表(){;}
内联点列表(常量QList&points):QList(点){;}
布尔哈斯(整数x)常数{
对于(const Point&p:*本)
如果(p.x()==x)
返回true;
返回false;
}
};
如果我编译上面的代码,会出现以下错误:

1>C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(111): error C2665: 'qHash' : none of the 25 overloads could convert all the argument types
1>          c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(372): could be 'uint qHash(const QTime &,uint) throw()'
1>          c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(371): or       'uint qHash(const QDate &,uint) throw()'
1>          c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(370): or       'uint qHash(const QDateTime &,uint)'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qlocale.h(62): or       'uint qHash(const QLocale &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qregexp.h(56): or       'uint qHash(const QRegExp &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(101): or       'uint qHash(QLatin1String,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(100): or       'uint qHash(const QBitArray &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(99): or       'uint qHash(const QStringRef &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(98): or       'uint qHash(const QString &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(97): or       'uint qHash(const QByteArray &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(96): or       'uint qHash(const QChar,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(94): or       'uint qHash(long double,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(92): or       'uint qHash(double,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(91): or       'uint qHash(float,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(90): or       'uint qHash(qint64,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(86): or       'uint qHash(quint64,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(85): or       'uint qHash(long,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(79): or       'uint qHash(ulong,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(78): or       'uint qHash(int,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(77): or       'uint qHash(uint,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(76): or       'uint qHash(short,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(75): or       'uint qHash(ushort,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(74): or       'uint qHash(signed char,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(73): or       'uint qHash(uchar,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(72): or       'uint qHash(char,uint) throw()'
1>          while trying to match the argument list '(const Point)'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(920) : see reference to function template instantiation 'uint qHash<Key>(const T &,uint)' being compiled
1>          with
1>          [
1>              Key=Point
1>  ,            T=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(916) : while compiling class template member function 'QHashNode<Key,T> **QHash<Key,T>::findNode(const Key &,uint *) const'
1>          with
1>          [
1>              Key=Point
1>  ,            T=QHashDummyValue
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(760) : see reference to function template instantiation 'QHashNode<Key,T> **QHash<Key,T>::findNode(const Key &,uint *) const' being compiled
1>          with
1>          [
1>              Key=Point
1>  ,            T=QHashDummyValue
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(756) : while compiling class template member function 'QHash<T,QHashDummyValue>::iterator QHash<T,QHashDummyValue>::insert(const Key &,const QHashDummyValue &)'
1>          with
1>          [
1>              T=Point
1>  ,            Key=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(205) : see reference to function template instantiation 'QHash<T,QHashDummyValue>::iterator QHash<T,QHashDummyValue>::insert(const Key &,const QHashDummyValue &)' being compiled
1>          with
1>          [
1>              T=Point
1>  ,            Key=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(251) : see reference to class template instantiation 'QHash<T,QHashDummyValue>' being compiled
1>          with
1>          [
1>              T=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(381) : see reference to class template instantiation 'QSet<T>' being compiled
1>          with
1>          [
1>              T=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(381) : while compiling class template member function 'QSet<T> QList<T>::toSet(void) const'
1>          with
1>          [
1>              T=Point
1>          ]
1>          c:\projects\points\point.h(139) : see reference to class template instantiation 'QList<Point>' being compiled
1>C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(111):错误C2665:“qHash”:25个重载中没有一个可以转换所有参数类型
1> c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(372):可以是“uint qHash(const QTime&,uint)throw()”
1> c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(371):或“uint qHash(const QDate&,uint)throw()”
1> c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(370):或“uint qHash(const qdatetime&,uint)”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qlocale.h(62):或“uint-qHash(const-qlocale&,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qregexp.h(56):或“uint qHash(const qregexp&,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(101):或“uint-qHash(QLatin1String,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(100):或“uint-qHash(const-qbitary&,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(99):或“uint-qHash(const-QStringRef&,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(98):或“uint-qHash(const-QString&,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(97):或“uint-qHash(const-QByteArray&,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(96):或“uint qHash(const QChar,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(94):或“uint-qHash(long-double,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(92):或“uint-qHash(double,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(91):或“uint-qHash(float,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(90):或“uint-qHash(qint64,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(86):或“uint-qHash(quint64,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(85):或“uint-qHash(long,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(79):或“uint-qHash(ulong,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(78):或“uint-qHash(int,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(77):或“uint-qHash(uint,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(76):或“uint-qHash(short,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(75):或“uint-qHash(ushort,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(74):或“uint-qHash(signed char,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(73):或“uint-qHash(uchar,uint)throw()”
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(72):或“uint-qHash(char,uint)throw()”
1> 尝试匹配参数列表(常量点)时
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(920):请参阅正在编译的函数模板实例化“uint qhash(const T&,uint)”的参考
1> 与
1>          [
1> 关键点
1> ,T=点
1>          ]
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(916):编译类模板成员函数'QHashNode**qhash::findNode(const Key&,uint*)const'
1> 与
1>          [
1> 关键点
1> ,T=QHashDummyValue
1>          ]
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(760):请参阅正在编译的函数模板实例化'QHashNode**qhash::findNode(const Key&,uint*)const'
1> 与
1>          [
1> 关键点
1> ,T=QHashDummyValue
1>          ]
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(756):编译类模板成员函数“qhash::iterator qhash::insert(const Key&,const QHashDummyValue&)”时
1> 与
1>          [
1> T=点
1> ,Key=Point
1>          ]
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(205):请参阅正在编译的函数模板实例化“QHash::iterator QHash::insert(const Key&,const QHashDummyValue&)”的参考
1> 与
1>          [
1> T=点
1> ,Key=Point
1>          ]
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(251):请参阅正在编译的类模板实例化“QHash”的参考
1> 与
1>          [
1> T=点
1>          ]
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(381):请参阅正在编译的类模板实例化“qset”的参考
1> 与
1>          [
1> T=点
1>          ]
1> C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(381):编译类模板成员函数“qset QList::toSet(void)const”时
1> 与
1>          [
1> T=点
1>          ]
1> c:
#include <QtCore>

class Point
{
public:
   // all default constructors are OK
   int x() const { return m_x; }
protected:
   int m_x = 0;
};
Q_DECLARE_METATYPE(Point)

class PointList : public QList<Point>
{
public:
   using QList::QList;
   inline PointList() = default;
   inline PointList(const QList<Point>& points) : QList<Point>(points) {}

   bool hasX(int x) const {
      for (auto & p: *this)
         if (p.x() == x)
            return true;
      return false;
   }
};

int main() {
   PointList p0;
   QList<Point> p1;
   PointList p2((QList<Point>{Point()}));
   PointList p3{Point()};
   PointList p4(p1);
   PointList p5(p2);
}
return std::find_if(begin(), end(), [x](const Point &p){ return p.x() == x; }) != end();