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. No appropriate default constructor available when defining a struct inside QMap
Servers for Qt installer are currently down

No appropriate default constructor available when defining a struct inside QMap

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.2k Views 2 Watching
  • 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.
  • L Offline
    L Offline
    lansing
    wrote on 26 Jul 2020, 12:50 last edited by lansing
    #1

    I have a list of one key multiple values to insert into a container.

    id: "png", description: "this is png", quality: 100
    id: "jpg", description: "this is jpg", quality: 0
    

    id is the key and description and quality are the values. I tried to do this with QMap with a struct at the value position

        struct ImageProp {
            QString description;
            int quality;
    
            ImageProp(QString d, int qu)
                : description(d), quality(qu)
            {}
        };
    
        typedef QMap<QString, ImageProp> ImageFormat;
        ImageFormat imageFormatMap;
    
        imageFormatMap.insert("png", ImageProp("this is png", 100));
    
    

    The editor detected no error, but when I ran it I got the errors:

    'MyClass::ImageProp' no apprpriate default constructor available
    
    C:\Qt\5.15.0\msvc2019_64\include\QtCore\qmap.h:679: error: C2664: 'void QMap<QString, MyClass::ImageProp>::insert(const QMap<QString, MyClass::ImageProp> &)': cannot convert argument 1 from 'const Key' to 'const QMap<QString,MyClass::ImageProp> &'
    with
    [
       Key=QString
    ]
    

    For some reason it's not reading my constructor.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Jul 2020, 13:46 last edited by
      #2

      Hi,

      A default constructor is a constructor that requires no parameters to be used. So it either have no parameters or all parameters have a default value.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 26 Jul 2020, 14:42
      1
      • S SGaist
        26 Jul 2020, 13:46

        Hi,

        A default constructor is a constructor that requires no parameters to be used. So it either have no parameters or all parameters have a default value.

        L Offline
        L Offline
        lansing
        wrote on 26 Jul 2020, 14:42 last edited by
        #3

        @SGaist

        Thanks, adding a blank constructor for the struct solved it.

            struct ImageProp {
                QString description;
                int quality;
        
                ImageProp() {}
        
                ImageProp(QString d, int qu)
                    : description(d), quality(qu)
                {}
            };
        
        C 1 Reply Last reply 26 Jul 2020, 17:13
        0
        • L lansing
          26 Jul 2020, 14:42

          @SGaist

          Thanks, adding a blank constructor for the struct solved it.

              struct ImageProp {
                  QString description;
                  int quality;
          
                  ImageProp() {}
          
                  ImageProp(QString d, int qu)
                      : description(d), quality(qu)
                  {}
              };
          
          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 26 Jul 2020, 17:13 last edited by Christian Ehrlicher
          #4

          @lansing This leaves quality uninitialzed...

          This is much better, it also avoids one useless copy

          struct ImageProp {
            QString description;
            int quality = 0;
            ImageProp() = default;
            ImageProp(const QString &d, int qu)
              : description(d), quality(qu)
            {}
          };
          

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          L 1 Reply Last reply 26 Jul 2020, 17:27
          1
          • C Christian Ehrlicher
            26 Jul 2020, 17:13

            @lansing This leaves quality uninitialzed...

            This is much better, it also avoids one useless copy

            struct ImageProp {
              QString description;
              int quality = 0;
              ImageProp() = default;
              ImageProp(const QString &d, int qu)
                : description(d), quality(qu)
              {}
            };
            
            L Offline
            L Offline
            lansing
            wrote on 26 Jul 2020, 17:27 last edited by
            #5

            @Christian-Ehrlicher

            Thanks I'll do that. And why the use of QString &d instead of QString d in the argument?

            S 1 Reply Last reply 26 Jul 2020, 18:30
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 26 Jul 2020, 17:42 last edited by
              #6

              @lansing said in No appropriate default constructor available when defining a struct inside QMap:

              And why the use of QString &d instead of QString d in the argument?

              To avoid a copy of the QString object

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              L 1 Reply Last reply 26 Jul 2020, 23:33
              0
              • L lansing
                26 Jul 2020, 17:27

                @Christian-Ehrlicher

                Thanks I'll do that. And why the use of QString &d instead of QString d in the argument?

                S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 26 Jul 2020, 18:30 last edited by
                #7

                @lansing said in No appropriate default constructor available when defining a struct inside QMap:

                @Christian-Ehrlicher

                Thanks I'll do that. And why the use of QString &d instead of QString d in the argument?

                The const is important in const QString &

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • C Christian Ehrlicher
                  26 Jul 2020, 17:42

                  @lansing said in No appropriate default constructor available when defining a struct inside QMap:

                  And why the use of QString &d instead of QString d in the argument?

                  To avoid a copy of the QString object

                  L Offline
                  L Offline
                  lansing
                  wrote on 26 Jul 2020, 23:33 last edited by
                  #8

                  @Christian-Ehrlicher @SGaist

                  Okay I got it. I'll use const QString & from now on.

                  1 Reply Last reply
                  0

                  1/8

                  26 Jul 2020, 12:50

                  • Login

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