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

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 last edited by BoGut
    #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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      4
      • mrjjM mrjj

        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 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
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on 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
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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

            • Login

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