Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: no matching function for call to ... at return statement

I'm using GCC7 on Qt 5.9.4 on openSUSE Leap 15.

I have the following 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;

}

With the following implementations for constructors:

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(){}

I'm receiving the following error:

error: no matching function for call to ‘ManSuppProps::ManSuppProps(ManSuppProps&)’

At a method of another class which has a member of class ManSuppProps:

ManSuppProps EditorScene::manSuppProps()
{
    return m_manSuppProps; // error is thrown here
}

Considering I have all the constructors, I don't get why the error is received. Can anybody help.

like image 203
user3405291 Avatar asked Jan 28 '23 11:01

user3405291


1 Answers

This is expected behavior. Note that the appropriate constructor is declared as explicit as

explicit ManSuppProps(ManSuppProps &manSuppProps);

And return m_manSuppProps; performs copy initialization,

4) when returning from a function that returns by value

And copy-initialization doesn't consider explicit constructors.

(emphasis mine)

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.

like image 124
songyuanyao Avatar answered Feb 16 '23 04:02

songyuanyao