Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Basic(?) problem with invalid use of non-static data member

Basic(?) problem with invalid use of non-static data member

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 7.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tomas373
    wrote on 9 Dec 2012, 12:23 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 9 Dec 2012, 13:00 last edited by
      #2

      @
      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.

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        terenty
        wrote on 9 Dec 2012, 13:14 last edited by
        #3

        @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)

        1 Reply Last reply
        0

        1/3

        9 Dec 2012, 12:23

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved