lundi 27 mai 2019

Using int and nullptr as default parameters is not working [duplicate]

This question already has an answer here:

I am using MSVC2015 compiler in QtCreator.

A class constructor takes two default parameters

class EditorChannel : public QObject
{
    Q_OBJECT
public:
    explicit EditorChannel(int localPort = 31415, QObject *parent = nullptr);
    ...
}

When I try to create an object with one (pointer) parameter like this:

editorChannel = new EditorChannel(this);

I am getting this error:

error: C2664: 'EditorChannel::EditorChannel(const EditorChannel &)': cannot convert argument 1 from 'MyObject *const ' to 'int'

As I know nullptr is not convertable to int. Why compiler cannot resolve constructor's default parameters int and nullptr?

Right now I have one more constructor with one parameter, but it's strange to me that above constructor is not working.

Aucun commentaire:

Enregistrer un commentaire