views:

88

answers:

5

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?

+4  A: 

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.

James McNellis
Minor nit: You should pass `this` instead of `parent` to `line1()`.
Bill
I did, it didn't help.
Blin
+1  A: 

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);
}
Danvil
A: 

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);
}
CDSO1
What is this modded down for?
CDSO1
I wonder as well, i indeed forgot to use a pointer. Althoug compiler now has different error"ISO C++ forbids declaration of 'saveLine' with no type" and"expected ';' before '*' token"
Blin
Perhaps you are missing your #include directive?
CDSO1
No, it's exactly where it should be.
Blin
+1  A: 

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;
};
extropy
A: 

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

David Walthall