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.
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 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);
}
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
Related
I wanted to declare a QGraphicsView which has a previously declared member as an argument of the constructor, but the compiler interprets it as a function.
(Code for reference)
class Widnow : public QMainWindow
{
Q_OBJECT
// constructors, member functions, etc...
private:
Ui::Widnow *ui;
QTimer timer01;
QGraphicsScene gaem;
QGraphicsView wiev(&gaem); //this gets interpreted as a function
}
Trying to call the constructor as QGraphicsView wiev = QGraphicsView(&gaem); also causes an error, as the copy constructor has been deleted... Is there a way to declare this member without errors?
The C++ compiler thinks that it is a method declaration where "wiev" as method that returns an instance of QGraphicsView. Use this way
class Widnow : public QMainWindow
{
private:
QGraphicsScene gaem;
QGraphicsView wiev; //this gets interpreted as a function
public:
Widnow() : wiev(&gaem)
{}
};
I read that the signal/slot concept in qt should always pass arguments by value instead of reference to ensure that signals/slots work flawlessly between threads.
I now have a piece of code that will only compile when the argument to a signal is emitted by reference, not value:
#include <QObject>
class mythirdclass {
public:
mythirdclass();
};
class mysecondclass : public QObject, public mythirdclass {
public:
mysecondclass(mythirdclass third);
};
class myclass : public QObject {
Q_OBJECT
public:
myclass();
signals:
// not working
void messageReceived(mysecondclass mymessage);
// working
// void messageReceived(mysecondclass &mymessage);
};
myclass::myclass()
{
mythirdclass third;
mysecondclass msg(third);
emit messageReceived(msg);
}
mysecondclass::mysecondclass(mythirdclass third)
{
// DO stuff
}
mythirdclass::mythirdclass()
{
}
The compiler error is:
..\example\main.cpp: In constructor 'myclass::myclass()':
..\example\main.cpp:28:20: error: use of deleted function 'mysecondclass::mysecondclass(const mysecondclass&)'
emit signal(second);
^
..\example\main.cpp:8:7: note: 'mysecondclass::mysecondclass(const mysecondclass&)' is implicitly deleted because the default definition would be ill-formed:
class mysecondclass : QObject, public mythirdclass {
^
Based on the errors I thought abour writing a copy constructor for mysecondclass, however after some attempts I gave up for now, because I didn't get it right.
So my questions are:
why is the compiling failing in the first place?
if it fails because of a missing copy constructor, why is the compiler not able to define one implicitly?
how would the working copy constructor in my case look like?
Thanks in advance.
Why is the compiling failing in the first place?
Because passing by value implies copying, and if the copy-constructor is deleted, then it simply can't pass by value and your function cannot be compiled with this signature, while it can receive its argument by reference because it doesn't involve copying.
If it fails because of a missing copy constructor, why is the compiler not able to define one implicitly?
It actually fails because it hasn't been able to define one implicitly. The reason for that is that your class derives from QObject. And that QObject doesn't have a public or protected copy-constructor, by design. So the compiler cannot define one implicitly.
How would the working copy constructor in my case look like?
Given the nature of QObjects, and the design decision that's behind it when it comes to QObjects not being copyable, I would recommend against using signals and slots that take QObjects or class deriving from it by value (or simply any function that does this, and signals/slots are mainly functions deep down), but instead by reference or by pointer.
For you third question, if you do not want to define a default constructor for mysecondclass, your copy constructor would probably look like
mysecondclass(mysecondclass const &other) : mythirdclass() {
/// stuff
}
For your second question, I suppose (not sure) that the default copy constructor try to use the default constructor that has been automatically deleted because you have defined another constructor of mysecondclass from an object of mythirdclass (I would recommend to you to use a const reference for the parameter here to avoid a useless copy)
I have read many threads regarding this, but I couldn't find an answer to this
In my Qt application, I am using QSignalSpy to catch a signal. It has a user-defined datatype for one of its parameters. To catch such a parameter, I have to first register that datatype with Qt using QMetaType and using the macro Q_DECLARE_METATYPE. It says
This macro makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the type Type as a custom type in QVariant.
The problem: I have a class CustomData with only constructor defined. Now unless I explicitly declare a destructor and a copy constructor, Qt throws an error. I want to use the implicit destructor and copy constructor that C++ gives. For destructor, I used
~CustomData() = default;
which uses the default destructor. But I cannot use a similar statement for the copy constructor. Will using
CustomData( const CustomData& ) {};
invoke the implicit copy constructor?
(I am doing this because I want to retain the behavior of the implicit copy constructor)
Thanks in advance.
The CustomData class is shown below
#include <QMetaType>
#include <QString>
class CustomData : public QObject
{
Q_OBJECT
public:
CustomData(QObject *parent = NULL);
~CustomData() = default; // I added this line
//Will the next line call the implicit copy constructor?
CustomData(const CustomData&) {}; //I added this line
enum CustomMode {mode1, mode2, mode3};
void somePublicMethod();
signals:
void completed(CustomData *data);
private slots:
void customComplete();
private:
CustomMode _mode;
QString _path;
CustomData *_chained;
};
Q_DECLARE_METATYPE(CustomData)
In short - it doesn't matter. Your implementation does not(hopefully) cause any effect.
If you go through the Qt documentation, it says as follows
QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private.
Go through this -- Q_DISABLE_COPY().
This dictates that you use pointers to do the work. Look at this thread which discusses on how to copy object in Qt.
Actually the given code compiles without errors on Visual Studio 2013. However if the signature of the signal is changed to void completed(CustomData data); then a similar error occurs. Since you have not specified the compiler, but I assume it is gcc/clang you could get the error because template handling differs a bit. If so, the error is most likely caused by Q_DECLARE_METATYPE(CustomData) since it tries to generate the copy constructor inside. Change it to Q_DECLARE_METATYPE(CustomData*) (because that is what you really need, you are queuing arguments of type CustomData* not CustomData, at least in the given sample) and if you really have no signals that send the CustomData instances by value you should be fine.
Also, if you are passing the object by pointer then you should use Q_DECLARE_METATYPE(CustomData*). But I'd advise passing it through a std::unique_ptr or std::shared_ptr to prevent memory leaks.
I am trying to understand the following code.(I am learning C++)
class DefaultDevice : public Device {
public:
DefaultDevice() :
ui(new DefaultUI) {
}
private:
RecoveryUI* ui;
};
class DefaultUI : public ScreenRecoveryUI {
...
}
I am having little trouble understanding ui(new DefaultUI) part.
As I understand it is part of initialization before DefaultDevice() constructor is executed.
Then, from my understanding, it is going to call ReocoveryUI constructor with new DefaultUI argument. But, RecoveryUI class does not have any constructor with such argument.(sorry for not posing RecoveryUI class. it's too long :( if anyone interested in, it is Android Open source code)
so what does this 'new DefaultUI' do?
If that is a valid and working code, It seems RecoveryUI is a base class for DefaultUI.
ui(new DefaultUI) creates an object and assigns it to ui.
So, ui points to its child object.
It is not run before the ctor, it is part of the ctor. It's called an initializer list and it's used to initialize the (non-static) member variables of a class.
I just have those questions about those code sinppets from the C++ GUI Programming with Qt 4 book:
GoToCellDialog::GoToCellDialog(QWidget *parent):QDialog(parent)
Does that mean we are inheriting QDialog(parent)? Or, what exactly does this mean?
setupUi(this);
Here, this code snippet is part of the gotocelldialog.cpp file, which is the implementation of gotocelldialog.h header file. What do we mean by this in this context? What are we trying to setup? And, what kind of setup will that be?
Thanks.
GoToCellDialog::GoToCellDialog(QWidget *parent) : QDialog(parent)
: signifies an initializer list. And it meant, parent is being passed as a parameter to QDialog constructor. I assume GoToCellDialog is derived from QDialog and so sending parent to it's constructor. So, before even body of GoToCellDialog is executed, QDialog constructor is executed.
This example should give you an idea -
class foo
{
int number ;
public:
foo(int i) : number(i) // Means copying value of i to number
{}
};
class bar : public foo
{
public:
bar(int temp) : foo(temp)
{ // <- Before getting here, foo sub object must be constructed.
// Because the order of construction takes from parent to child.
}
};
In the above example, definitely an argument for foo constructor must be passed while instantiation of bar. So, initializer list is the only way because there is no default constructor( i.e., constructor with no arguments) available for foo.
To answer your questions:
Does that mean we are inheriting QDialog(parent)?
Yes, this is basic C++ inheritance.
setupUi(this);
In short: The 'User Interface compiler' (uic) compiles/translates the xml file to C++ code which will be compiled and linked. The setupUi() function ensures that the Qt designer widgets you made (generated C++ code) are setup to be used by your code by Building the Widget tree.