我收到此错误:
/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*’
^
编译constructor
类的Company
代码时:
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
{
// .... some intialization stuff here, unrelated to the error
}
它只发生在父级为QAbstractItemModel
的类中。对于其他类,QObject
的父级我正在使用初始化列表而没有错误。添加初始化列表时出现错误。
这是在公司类中声明search_results
的方式:
class Company : public QObject {
...
DepList search_results;
...
}
它的构造函数是:
DepList::DepList(QAbstractItemModel *parent,Company* p_company) : QAbstractItemModel(parent)
{
company=p_company;
}
这里有什么问题,为什么它只发生在QAbstractItemModel
? QAbstractItemModel
也是QObject
的子项,我对QObject
类中的初始化列表没有任何问题。我考虑过铸造,但它会安全吗?
Company
是QObject
- 派生的:
class Company : public QObject {
Q_OBJECT
...
explicit Company(QObject *parent = 0);
...
};
但DepList
是QAbstractItemModel
- 派生的:
class DepList : public QAbstractItemModel
{
...
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
...
}