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. Class instance is uninitialized
QtWS25 Last Chance

Class instance is uninitialized

Scheduled Pinned Locked Moved Solved General and Desktop
c++qmlclassinstancec4700
5 Posts 3 Posters 1.1k 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.
  • B Offline
    B Offline
    BoGut
    wrote on 1 Aug 2020, 21:03 last edited by BoGut 8 Jan 2020, 21:04
    #1

    Hello fellow QT'ers, this is such a weird error. It's crashing my app on run. I get a C4700 uninitialized local variable used. There's no default constructor as the data comes from an external file. In QML, I'm able to console.log(data.name) and it's value is displayed in the application window. Any ideas?

    Data.h
    ------------------------------------------------------------------------
    class Data : public QObject {
        Q_OBJECT
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    public:
        Data(int number);
        ~Data();
        QString name() const;
    public slots:
        void setName(QString name);
    signals:
        void nameChanged(QString name);
    private:
        QString m_name;
    };
    
    Main.cpp
    ------------------------------------------------------------------------
    void Main::testRun()
    {
        Data* data;
        QString name01 = data->name();
        qDebug() << "111: " << data->name();  *** this is the line that has the error.
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Aug 2020, 21:51 last edited by
      #2

      Hi
      Is this the actual code ?
      You have
      Data* data;
      which is a dangling pointer as data points to nothing valid.
      It has t0 be
      Data* data = new Data;

      B 1 Reply Last reply 2 Aug 2020, 00:40
      4
      • M mrjj
        1 Aug 2020, 21:51

        Hi
        Is this the actual code ?
        You have
        Data* data;
        which is a dangling pointer as data points to nothing valid.
        It has t0 be
        Data* data = new Data;

        B Offline
        B Offline
        BoGut
        wrote on 2 Aug 2020, 00:40 last edited by
        #3

        @mrjj My apology, I forgot to mention that Data has no default constructor.

        Data* data = new Data; **** I get the error "no matching constructor for initialization of Data"
        
        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 2 Aug 2020, 03:33 last edited by
          #4

          Do you undertand the C++ programming language? Given the class definition as shown, of course its going to fail. If new Data fails, then given the class definition what kind of new won't fail?

          1 Reply Last reply
          3
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 2 Aug 2020, 07:50 last edited by
            #5

            Hi
            You always have to initialize a pointer.
            Else it will crash on use.
            Hint:
            There is only one constructor and it takes a parameter :)

            1 Reply Last reply
            4

            2/5

            1 Aug 2020, 21:51

            • Login

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