Basic(?) problem with invalid use of non-static data member
Hello, I need help with my code,
I've got three classes. The first is: (the two objects of this class will be the part of QDialog from class mru)
@class MruaLineEdit : public QWidget
The class mru:
class mru : public QDialog
mru(QWidget *parent = 0);
MruaLineEdit *mruaWidget; //<<=== These two objects
Now, I would like to change the value of "x0" of another third class in an inline function:
float x, x0, v, v0, a, t, t0;
x0 = mru::mruaWidget->xInicialLineEdit->text().toFloat(&ok); //<<=== Surely, here is the problem
When I'm trying to compile the code, I've got the error:
/home/tomasz/unit_converter/unit_converter/mru.h:32: błąd:invalid use of non-static data member 'mru::mruaWidget'.
I thing I've tried anything that my small brain can understand, and I'm really tired of continuing finding the solution. I just realized that I'm not able to do it by myself. Help me please, and thank you for any useful advice.
mru *myMru = new mru();
x0 = myMru->mruaWidget->xInicialLineEdit->text().toFloat(&ok);
You need to initialize the object first. And in order not to loose the data when going out of scope, you can do it on class level. The above code is only a snippet that will hopefully show you the error. This is not a real solution, this requires a bit more refactoring. You can pass a pointer to the mru object to the function, for example. Or declare mru inside the class and set it in the constructor.
@x0 = mru::mruaWidget->xInicialLineEdit->text().toFloat(&ok);@
you are trying to access data without an instance of the class; this is only possible if this data is declared as static:
@static MruaLineEdit *mruaWidget; //<<=== These two objects@
Or another way you should instantiate your class and access this data througt this instance:
@mru *mruInstance = new mru();
it's not clear what youre trying to achive though :)
P.S. static data is shared among all instances (and even exist without any instance) while non static data is specific for every instance of a class and does not exist without an object (instance)