简体   繁体   中英

Qt “no matching function for call”

I have

no matching function for call to 'saveLine::saveLine()'

error when compiling my application. The construcor is never actually called.

saveLine class definition:

class saveLine
{
public:
    saveLine(QWidget *parent);
private:
    QPushButton *selectButton, *acceptButton;
    QLabel *filePath;
    QLineEdit *allias;
};

saveLine is used in another class which is defined as follows:

class MWindow : public QWidget
{
    Q_OBJECT
public:
    MWindow(QWidget *parent=0);
private:
    saveLine line1;
};

error points to MWindow constructor implementation

MWindow::MWindow(QWidget *parent):QWidget(parent)
{
    this->setWindowTitle("Launcher");
    this->resize(600,600);
}

What should i do? I intend to use saveLine class in a vector, to create lines at runtime.

EDIT: i've misdeclared line1, it should read

saveLine *line1;

but now it gives another error

ISO C++ forbids declaration of 'saveLine' with no type

and

expected ';' before '*' token

on this line. It seems like saveLine is no longer considered a class, how so?

Since you provide a user-declared constructor for the class saveLine , the default constructor is not provided by the compiler. Your constructor is not a default constructor (it has one required parameter), so you cannot default construct an object of type saveLine .

Since you have a saveLine object in your MWindow class, you need to initialize it using your constructor, in the MWindow constructor's initializer list:

MWindow::MWindow(QWidget *parent)
    : QWidget(parent), line1(parent)
{
    //...
}

(I'm assuming that parent pointer is the one you mean to pass; if you need to give it something else, then give it what it needs)

Another option would be to provide a default argument for the parameter in saveLine 's constructor:

saveLine(QWidget *parent = 0);

This would allow that constructor to be called with no arguments (and would make it a default constructor). Whether this makes sense to do depends on whether the parent pointer really is optional. Obviously, if you do this, you'll need to check to be sure the pointer is not null before you dereference and use it.

You have to call the constructor of saveLine in the constructor of MWindow giving him the desired parent.

Use:

MWindow::MWindow(QWidget *parent) : QWidget(parent), line1(parent)
{
    this->setWindowTitle("Launcher");
    this->resize(600,600);
}

but now it gives another error

ISO C++ forbids declaration of 'saveLine' with no type

You need to add a forward declaration to tell the compiler that saveLine class exists:

Like this:

//declare that there will be a class saveLine
class saveLine;

class MWindow : public QWidget
{
    Q_OBJECT
public:
    MWindow(QWidget *parent=0);
private:
    saveLine *line1;
};

You are declaring an instance of saveLine in the class declaration, instead of a pointer to a saveLine.

You could change the reference in MWindow to

saveLine* line1;

OR

you could implement like this:

MWindow::MWindow(QWidget *parent):QWidget(parent), line1(parent)
{
    this->setWindowTitle("Launcher");
    this->resize(600,600);
}

Try adding class saveLine; just above the class MWindow line. This often occurs when the .h files for the saveLine class and the MWindow class include each other (either directly or indirectly). For example, see the first few posts of http://www.allegro.cc/forums/thread/594307

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM