error: no matching function for call to ... at return statement
error: no matching function for call to ... at return statement
我在 openSUSE Leap 15 上的 Qt 5.9.4 上使用 GCC7。
我有以下 class:
class ManSuppProps : public QObject
{
Q_OBJECT
public:
explicit ManSuppProps(QString parentName);
explicit ManSuppProps(){}
explicit ManSuppProps(const ManSuppProps &manSuppProps);
explicit ManSuppProps(ManSuppProps &manSuppProps);
~ManSuppProps();
private:
QVector3D m_suppPos;
QString m_suppParentName;
}
使用以下构造函数实现:
ManSuppProps::ManSuppProps(QString parentName)
: QObject()
, m_suppPos(QVector3D(0, 0, 0))
, m_suppParentName(parentName)
{
qDebug()<<"Constructing ManSuppProps object ...";
}
ManSuppProps::ManSuppProps(const ManSuppProps &manSuppProps)
: QObject()
, m_suppPos(manSuppProps.getSuppPos())
, m_suppParentName(manSuppProps.getSuppParentName())
{
}
ManSuppProps::ManSuppProps(ManSuppProps &manSuppProps)
: QObject()
, m_suppPos(manSuppProps.getSuppPos())
, m_suppParentName(manSuppProps.getSuppParentName())
{
}
ManSuppProps::~ManSuppProps(){}
我收到以下错误:
error: no matching function for call to ‘ManSuppProps::ManSuppProps(ManSuppProps&)’
另一个 class 的方法中有 class ManSuppProps
的成员:
ManSuppProps EditorScene::manSuppProps()
{
return m_manSuppProps; // error is thrown here
}
考虑到我拥有所有的构造函数,我不明白为什么会收到错误。谁能帮忙
这是预期的行为。请注意,适当的构造函数声明为 explicit
as
explicit ManSuppProps(ManSuppProps &manSuppProps);
并且return m_manSuppProps;
执行copy initialization,
4) when returning from a function that returns by value
并且复制初始化不考虑 explicit
构造函数。
(强调我的)
If T
is a class type and the cv-unqualified version of the type of other
is T
or a class derived from T
, the non-explicit constructors of T
are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
Copy-initialization is less permissive than direct-initialization: explicit constructors are not converting constructors and are not considered for copy-initialization.
我在 openSUSE Leap 15 上的 Qt 5.9.4 上使用 GCC7。
我有以下 class:
class ManSuppProps : public QObject
{
Q_OBJECT
public:
explicit ManSuppProps(QString parentName);
explicit ManSuppProps(){}
explicit ManSuppProps(const ManSuppProps &manSuppProps);
explicit ManSuppProps(ManSuppProps &manSuppProps);
~ManSuppProps();
private:
QVector3D m_suppPos;
QString m_suppParentName;
}
使用以下构造函数实现:
ManSuppProps::ManSuppProps(QString parentName)
: QObject()
, m_suppPos(QVector3D(0, 0, 0))
, m_suppParentName(parentName)
{
qDebug()<<"Constructing ManSuppProps object ...";
}
ManSuppProps::ManSuppProps(const ManSuppProps &manSuppProps)
: QObject()
, m_suppPos(manSuppProps.getSuppPos())
, m_suppParentName(manSuppProps.getSuppParentName())
{
}
ManSuppProps::ManSuppProps(ManSuppProps &manSuppProps)
: QObject()
, m_suppPos(manSuppProps.getSuppPos())
, m_suppParentName(manSuppProps.getSuppParentName())
{
}
ManSuppProps::~ManSuppProps(){}
我收到以下错误:
error: no matching function for call to ‘ManSuppProps::ManSuppProps(ManSuppProps&)’
另一个 class 的方法中有 class ManSuppProps
的成员:
ManSuppProps EditorScene::manSuppProps()
{
return m_manSuppProps; // error is thrown here
}
考虑到我拥有所有的构造函数,我不明白为什么会收到错误。谁能帮忙
这是预期的行为。请注意,适当的构造函数声明为 explicit
as
explicit ManSuppProps(ManSuppProps &manSuppProps);
并且return m_manSuppProps;
执行copy initialization,
4) when returning from a function that returns by value
并且复制初始化不考虑 explicit
构造函数。
(强调我的)
If
T
is a class type and the cv-unqualified version of the type ofother
isT
or a class derived fromT
, the non-explicit constructors ofT
are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.Copy-initialization is less permissive than direct-initialization: explicit constructors are not converting constructors and are not considered for copy-initialization.