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. Qt 5.5 - QList variable causes compilation to fail
Forum Updated to NodeBB v4.3 + New Features

Qt 5.5 - QList variable causes compilation to fail

Scheduled Pinned Locked Moved General and Desktop
21 Posts 5 Posters 7.2k Views 4 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #2

    Your error may be coming line previous to this line. Just check. I don't see any issue with above line of code.

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    1 Reply Last reply
    4
    • K Offline
      K Offline
      knight556
      wrote on last edited by
      #3

      I wish it was that easy, but if that line is commented out, the rest of the program runs perfectly.

      mrjjM 1 Reply Last reply
      0
      • K knight556

        I wish it was that easy, but if that line is commented out, the rest of the program runs perfectly.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @knight556 said:

        And the
        #include <QList>
        #include <QString>

        are in the .H file ?

        with the class having the m_mylist?

        Are you using Creator and a pro file or how do you build the project ?

        1 Reply Last reply
        1
        • K Offline
          K Offline
          knight556
          wrote on last edited by
          #5

          The includes are in the .cpp file, and yes I'm using Creator with a pro file. I've noticed that if I declare a QList within a class method then it compiles just fine, it's only when I try and declare it as a member variable that it complains.

          mrjjM 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Hi and welcome to devnet,

            Not a direct answer but since you want a QList of QString, why not use QStringList ?

            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
            1
            • K knight556

              The includes are in the .cpp file, and yes I'm using Creator with a pro file. I've noticed that if I declare a QList within a class method then it compiles just fine, it's only when I try and declare it as a member variable that it complains.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @knight556
              Hi

              But if you declare m_list in the H file you must have
              #include <QList>
              #include <QString>

              in the H file (and not cpp file ) or the type is unknown :)
              Its not enough to include in CPP file.

              I have noticed that if I declare a QList within a class method ...

              Yes as you included the files in the cpp I guess.

              K 1 Reply Last reply
              1
              • K Offline
                K Offline
                knight556
                wrote on last edited by
                #8
                This post is deleted!
                1 Reply Last reply
                0
                • mrjjM mrjj

                  @knight556
                  Hi

                  But if you declare m_list in the H file you must have
                  #include <QList>
                  #include <QString>

                  in the H file (and not cpp file ) or the type is unknown :)
                  Its not enough to include in CPP file.

                  I have noticed that if I declare a QList within a class method ...

                  Yes as you included the files in the cpp I guess.

                  K Offline
                  K Offline
                  knight556
                  wrote on last edited by
                  #9

                  @mrjj Moving the includes to the .h file didn't help.

                  @SGaist Yes the QStringList does in fact work, but what I have failed to mention (for simplicity) is that this bug is occurring for all data types, eg. QList<int> fails as well. As it turns out I want to use my own custom class as the list type, but I can't hope to get that working until I get the QList working with more basic data types first!

                  Creator is clearly happy with QList being used, because if I start typing on the next line something like:
                  m_mylist.
                  the autocomplete/intellisense/whatever-it's-called will give me the option of append(), so something is working correctly!

                  mrjjM 1 Reply Last reply
                  0
                  • K knight556

                    @mrjj Moving the includes to the .h file didn't help.

                    @SGaist Yes the QStringList does in fact work, but what I have failed to mention (for simplicity) is that this bug is occurring for all data types, eg. QList<int> fails as well. As it turns out I want to use my own custom class as the list type, but I can't hope to get that working until I get the QList working with more basic data types first!

                    Creator is clearly happy with QList being used, because if I start typing on the next line something like:
                    m_mylist.
                    the autocomplete/intellisense/whatever-it's-called will give me the option of append(), so something is working correctly!

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @knight556

                    Oh ok. sorry for assuming it was that basic thing,

                    Can we try a simple test just to be sure compiler not funky?

                    If you create a new console project and then
                    right click -> add new -> Header file and call it thehappylist.h
                    and put code below in thehappylist.h

                    #ifndef THEHAPPYLIST
                    #define THEHAPPYLIST
                    
                    #include <QList>
                    #include <QString>
                    class Happy {
                    public:
                        QList<QString> m_list;
                    };
                    #endif // THEHAPPYLIST
                    

                    and then the main.cpp

                    #include "thehappylist.h"
                    int main(int argc, char *argv[])
                    {
                        Happy h;
                        h.m_list.append("test");
                    }
                    

                    And tell me if that compiles ?

                    1 Reply Last reply
                    1
                    • K Offline
                      K Offline
                      knight556
                      wrote on last edited by
                      #11

                      @mrjj said:

                      #include "thehappylist.h"
                      int main(int argc, char *argv[])
                      {
                      Happy h;
                      h.m_list.append("test");
                      }

                      Hi, yes it does compile... which is frustrating because that doesn't help find the problem.

                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        HI

                        Well, its not an include path thing then.
                        Since the mini sample works.

                        Would it be possible to post your code ?
                        I really can not get a clue of what could be wrong as you have the includes
                        and it all sounds ok.

                        1 Reply Last reply
                        1
                        • K Offline
                          K Offline
                          knight556
                          wrote on last edited by
                          #13

                          I'm working on quite a large project (it's basically a DICOM image viewer), but I will attempt to create a minimal working example.

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            knight556
                            wrote on last edited by
                            #14

                            OK now this is weird. I'm not sure how I came to this, but instead of writing:

                            QList<QString> m_mylist;

                            I write:

                            ::QList<QString> m_mylist

                            It compiles and works... however Qt Creator doesn't seem to like it as the terms QList and QString are not coloured purple as normal.
                            Any ideas why this works?

                            JKSHJ 1 Reply Last reply
                            0
                            • K knight556

                              OK now this is weird. I'm not sure how I came to this, but instead of writing:

                              QList<QString> m_mylist;

                              I write:

                              ::QList<QString> m_mylist

                              It compiles and works... however Qt Creator doesn't seem to like it as the terms QList and QString are not coloured purple as normal.
                              Any ideas why this works?

                              JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #15

                              @knight556 said:

                              OK now this is weird. I'm not sure how I came to this, but instead of writing:

                              QList<QString> m_mylist;

                              I write:

                              ::QList<QString> m_mylist

                              It compiles and works... however Qt Creator doesn't seem to like it as the terms QList and QString are not coloured purple as normal.
                              Any ideas why this works?

                              1. Did you redefine "QList" somewhere else in your code?
                              2. Does this only happen in this particular class, or does it also happen if you add a QList member to other classses?

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              1
                              • K Offline
                                K Offline
                                knight556
                                wrote on last edited by
                                #16

                                Well that got me thinking, so I had a look and for some reason in another one of my classes I had listed a forward reference to QList. I have now deleted that line of code and the rest of the program compiles and works as expected, without needing the extra :: bits at the start of the QList declaration!
                                So... clearly I don't understand forward references. Thanks for the help everyone!

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  It's pretty unusual to forward declare QList, why did you do that in the first place ?

                                  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
                                  1
                                  • K Offline
                                    K Offline
                                    knight556
                                    wrote on last edited by
                                    #18

                                    I was learning Qt through a series of Pluralsight videos and the instructor was basically forward declaring every class he was gonna use, so I got into that habit. Time to go back and remove some of that rubbish.

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #19

                                      Even 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
                                      1
                                      • K Offline
                                        K Offline
                                        knight556
                                        wrote on last edited by
                                        #20

                                        Yep even QString. Is forward referencing even necessary if you put all your includes in the header file? I always thought forward referencing was only needed if you had 2 classes that referenced one another, but of course 1 had to be defined before the other.

                                        JKSHJ 1 Reply Last reply
                                        0
                                        • K knight556

                                          Yep even QString. Is forward referencing even necessary if you put all your includes in the header file? I always thought forward referencing was only needed if you had 2 classes that referenced one another, but of course 1 had to be defined before the other.

                                          JKSHJ Offline
                                          JKSHJ Offline
                                          JKSH
                                          Moderators
                                          wrote on last edited by JKSH
                                          #21

                                          @knight556 said:

                                          Is forward referencing even necessary if you put all your includes in the header file?

                                          No. The header contains more information than the forward declaration, so it doesn't make sense to forward-declare when you already have the header.

                                          I always thought forward referencing was only needed if you had 2 classes that referenced one another, but of course 1 had to be defined before the other.

                                          That's only one use of forward declarations.

                                          It is good practice to minimize the amount of code that you #include inside your headers. When your project gets large, forward declarations really help to speed up compilation times. See http://stackoverflow.com/questions/4757565/c-forward-declaration

                                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                          1 Reply Last reply
                                          1

                                          • Login

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