C++ QAbstractItemModel派生类没有匹配的函数调用
我得到这个错误:C++ QAbstractItemModel派生类没有匹配的函数调用,c++,qt,C++,Qt,我得到这个错误: /MyApp/company.cpp:3: error: no matching function for call to ‘DepList::DepList(Company*, Company*)’ Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
/MyApp/company.cpp:3: error: no matching function for call to ‘DepList::DepList(Company*, Company*)’
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
^
/MyApp/company.h:14: In file included from ../../../../MyApp/company.h:14:0,
/MyApp/company.cpp:1: from ../../../../MyApp/company.cpp:1:
/MyApp/deplist.h:34: candidate: DepList::DepList(QAbstractItemModel*, Company*)
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
/MyApp/deplist.h:34: note: no known conversion for argument 1 from ‘Company*’ to ‘QAbstractItemModel*’
^
为公司
类编译构造函数
代码时:
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
{
// .... some intialization stuff here, unrelated to the error
}
它只发生在父类为qabstractemmodel
的类中。对于其他类,QObject
的父类,我使用的初始化列表没有错误。添加初始化列表时出现错误
这就是在公司类中声明搜索结果的方式:
class Company : public QObject {
...
DepList search_results;
...
}
它的构造函数是:
DepList::DepList(QAbstractItemModel *parent,Company* p_company) : QAbstractItemModel(parent)
{
company=p_company;
}
这里的问题是什么?为什么它只发生在qabstractemmodel
qabstractemmodel
也是QObject
的子类,我对QObject
类中的初始化列表没有问题。我曾想过铸造,但安全吗
公司
是一个质量对象
-衍生:
class Company : public QObject {
Q_OBJECT
...
explicit Company(QObject *parent = 0);
...
};
class DepList : public QAbstractItemModel
{
...
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
...
}
但是DepList
是一个QAbstractItemModel
派生的:
class Company : public QObject {
Q_OBJECT
...
explicit Company(QObject *parent = 0);
...
};
class DepList : public QAbstractItemModel
{
...
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
...
}
问题是,正如错误消息所说,
Company*
无法转换为qabstractemmodel*
公司
似乎与qabstractemmodel
无关,不是吗?@songyuanyao你是对的,不是。但是它的成员search\u results
是qabstractemodel的一个子类。并且两者都继承了QObject
。我正在公司
对象的构造函数中初始化搜索结果
,我知道您正在通过search\u results(this,this)
初始化搜索结果
,它正在调用DepList
的构造函数,该构造函数要求第一个参数是qabstractemmodel*
;但是您正在传递这个
,即公司*
。那么您希望编译器做什么呢?顺便说一句,传入未完全构造的对象的这个指针有点危险。有关详细信息,请参阅和。父项必须是qabstractemmodel
,但公司
不是。如果将Company
更改为继承自qabstractemmodel
而不是QObject
,则它将正常工作。