Results 1 to 7 of 7

Thread: warning in QDataStream template

  1. #1
    Join Date
    Jul 2012
    Posts
    248
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    29
    Thanked 15 Times in 14 Posts

    Default warning in QDataStream template

    I get a warning in the "QDataStream& operator>>(QDataStream& s, QVector<T>& v)" template function:

    Dewmfj4.png


    how can i resolve that warning? Unfortunately, i am unable to pin it down to a specific overload of the operator. It looks to me that at one point a QVector<> of pointers to serializable objects is serialized, but it seems to work ok.


    Qt: 4.8.4
    os: xp sp3
    ide: VS2008


    thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: warning in QDataStream template

    What is the error message?
    What is your T?
    Can it be default constructed?

    Cheers.
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: warning in QDataStream template

    There is no error message but a warning is displayed in the screen shot.
    "warning C4700: uninitialized local variable 't' used"
    Apparently this is treated as an error in MSVC 2012+, although that does not apply here.

    It seems erroneous to me given that t is of type T and constructed on the line immediately before the offending stream operator. If T were not default constructable then the preceding line would be an error if, indeed, you ever managed to compile a declaration of the QVector<T>.

  4. #4
    Join Date
    Jul 2012
    Posts
    248
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    29
    Thanked 15 Times in 14 Posts

    Default Re: warning in QDataStream template

    it compiles just fine, though i feed it to vs2012 later to see if that steers something up. Since the warning is only inside the template code you can see in the screenshot, i do not now what "T" refers to.


    edit: hm, looking at the comit log, i would appear someone serializes a Qvector<xxx*>, ie a vector of pointers. Does that work at all? Deserializing seems to be ok.... :S Could it be related?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: warning in QDataStream template

    Yes. If you declare a QVector<Thingy*> then "T" is "Thingy*". If you then rely on the default, templated, operator>>() then you get the warning. The code that generates the warning looks like:
    Qt Code:
    1. template<typename T>
    2. QDataStream& operator>>(QDataStream& s, QVector<T>& v)
    3. {
    4. v.clear();
    5. quint32 c;
    6. s >> c;
    7. v.resize(c);
    8. for(quint32 i = 0; i < c; ++i) {
    9. T t;
    10. s >> t;
    11. v[i] = t;
    12. }
    13. return s;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Line 9 and 10 effectively read:
    Qt Code:
    1. Thingy* t;
    2. s >> t;
    To copy to clipboard, switch view to plain text mode 
    Under these circumstances t is indeed not initialised (i.e. it's an invalid pointer). The program might compile but it will crash.

    You cannot persist a pointer between sessions and expect to get the object it pointed to back at the other end. You should be able to provide a specific implementation of:
    Qt Code:
    1. QDataStream& operator>>(QDataStream& s, QVector<Thingy*>& v);
    2. // and
    3. QDataStream& operator<<(QDataStream& s, const QVector<Thingy*>& v);
    To copy to clipboard, switch view to plain text mode 
    that the compiler should use in preference to the templated versions. These versions would have to arrange serialisation/deserialisation of the objects pointed to by each of the vector member pointers.

  6. #6
    Join Date
    Jul 2012
    Posts
    248
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    29
    Thanked 15 Times in 14 Posts

    Default Re: warning in QDataStream template

    thanks!


    I did supply the two functions and everything seems to work fine now. No more warning.


    Interestingly, i tried it several times and the deserialization of the pointer vector seemed to work just fine. Which is why i went on to look for the problem somewhere else.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: warning in QDataStream template

    The compiler will happily let your code write through the invalid pointer assuming there is actually an instance on the end of it. Eventually it will write somewhere that will trigger the termination of your program.

Similar Threads

  1. Best way to print using a pdf template
    By sfabel in forum Qt Programming
    Replies: 3
    Last Post: 31st March 2011, 08:15
  2. Replies: 4
    Last Post: 25th June 2010, 09:21
  3. using the subdirs Template
    By bhs-ittech in forum Newbie
    Replies: 2
    Last Post: 23rd November 2007, 10:45
  4. Template class
    By steg90 in forum General Programming
    Replies: 15
    Last Post: 12th June 2007, 20:49
  5. problem using template
    By mickey in forum General Programming
    Replies: 6
    Last Post: 18th November 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.