gpt4 book ai didi

c++ - 错误 : no matching function for call to . .. 在返回语句

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:29 24 4
gpt4 key购买 nike

我在 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&)’

在具有类 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

并且复制初始化不考虑显式构造函数。

(强调我的)

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.

关于c++ - 错误 : no matching function for call to . .. 在返回语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52272559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com