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
    {
    Q_OBJECT

    public:
    MruaLineEdit(QWidget *parent);

    public:
    QLineEdit *xInicialLineEdit;
    QLineEdit *vInicialLineEdit;
    QLineEdit *aInicialLineEdit;

    private:
    QGridLayout *layout;
    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QLabel *label4;
    };@

    The class mru:

    @
    class mru : public QDialog
    {
    Q_OBJECT

    public:
    mru(QWidget *parent = 0);
    MruaLineEdit *mruaWidget[2]; //<<=== These two objects
    @

    Now, I would like to change the value of "x0" of another third class in an inline function:
    @
    class coche
    {
    public:
    float x, x0, v, v0, a, t, t0;
    void saveValues()
    {
    bool ok;
    x0 = mru::mruaWidget[0]->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.


  • Moderators

    @
    void saveValues()
    {
    bool ok;
    mru *myMru = new mru();
    x0 = myMru->mruaWidget[0]->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[0]->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[2]; //<<=== These two objects@

    Or another way you should instantiate your class and access this data througt this instance:

    @mru *mruInstance = new mru();
    mruInstance->mruaWidget[0]->xInicialLineEdit->text().toFloat(&ok);@

    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)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.