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. Why report a error,winfinite recursion, when override QDataStream &operator<< and argument is QVector.

Why report a error,winfinite recursion, when override QDataStream &operator<< and argument is QVector.

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 696 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.
  • Crawl.WC Offline
    Crawl.WC Offline
    Crawl.W
    wrote on last edited by
    #1

    Code as following:

    typedef QVector<WellDataAnalyzeInfo> ExpAnalysisResult;//WellDataAnalyzeInfo is a custom struct
    
    QDataStream &operator<<(QDataStream& out, const Analysis::ExpAnalysisResult& info)
    {
        out << info;
        return out;
    }
    

    The following code will have the same problem:

    QDataStream &operator<<(QDataStream& out, const QVector<int>& info)
    {
    out << info;
        return out;
    }
    

    But the following is normal:

    struct ExpAnalysisResult
    {
        QVector<int> ddd;
    };
    
    QDataStream &operator<<(QDataStream& out, const Analysis::ExpAnalysisResult& info)
    {
        out << info.ddd;
        return out;
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @crawl-w said in Why report a error,winfinite recursion, when override QDataStream &operator<< and argument is QVector.:

      out << info;

      What do you think happens here?

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

      Crawl.WC 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        @crawl-w said in Why report a error,winfinite recursion, when override QDataStream &operator<< and argument is QVector.:

        out << info;

        What do you think happens here?

        Crawl.WC Offline
        Crawl.WC Offline
        Crawl.W
        wrote on last edited by
        #3

        @christian-ehrlicher Let each element in the container be written to a file respectively.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          No, it calls "QDataStream &operator<<(QDataStream& out, const Analysis::ExpAnalysisResult& info)" - why should it iterate over the container?

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

          Crawl.WC 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            No, it calls "QDataStream &operator<<(QDataStream& out, const Analysis::ExpAnalysisResult& info)" - why should it iterate over the container?

            Crawl.WC Offline
            Crawl.WC Offline
            Crawl.W
            wrote on last edited by
            #5

            @christian-ehrlicher "typedef QVector<WellDataAnalyzeInfo> ExpAnalysisResult;"Because ExpAnalysisResult is a QVector<T>.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @crawl-w said in Why report a error,winfinite recursion, when override QDataStream &operator<< and argument is QVector.:

              Because ExpAnalysisResult is a QVector<T>.

              But you give the compiler already an operator for this - namely 'QDataStream &operator<<(QDataStream& out, const Analysis::ExpAnalysisResult& info)' - so why should another one be used when it finds an exact match for it?

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

              Crawl.WC 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                To add to @Christian-Ehrlicher

                @crawl-w said in Why report a error,winfinite recursion, when override QDataStream &operator<< and argument is QVector.:

                QDataStream &operator<<(QDataStream& out, const Analysis::ExpAnalysisResult& info)
                {
                out << info;
                return out;
                }

                The goal of this operator implantation is to serialise the content of your Analysis::ExpAnalysisResult object. Just writing out << info will basically be the operator trying to call itself.

                There's no automagical way to serialise your object. At some point you have to write down which fields should go in the stream.

                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
                • Christian EhrlicherC Christian Ehrlicher

                  @crawl-w said in Why report a error,winfinite recursion, when override QDataStream &operator<< and argument is QVector.:

                  Because ExpAnalysisResult is a QVector<T>.

                  But you give the compiler already an operator for this - namely 'QDataStream &operator<<(QDataStream& out, const Analysis::ExpAnalysisResult& info)' - so why should another one be used when it finds an exact match for it?

                  Crawl.WC Offline
                  Crawl.WC Offline
                  Crawl.W
                  wrote on last edited by Crawl.W
                  #8

                  @christian-ehrlicher @SGaist But the second code snippet is wrong,the third code snippet right. The third code snippet's info.ddd as same as the second code snippet's info and the first. About WellDataAnalyzeInfo, I have a alone operator implantation.

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

                    As long as you declare, implement and register the QDataStream operator for your class. You don't have to implement anything special for QVector as it's already handled. You only have to care about your custom type.

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

                    Crawl.WC 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      As long as you declare, implement and register the QDataStream operator for your class. You don't have to implement anything special for QVector as it's already handled. You only have to care about your custom type.

                      Crawl.WC Offline
                      Crawl.WC Offline
                      Crawl.W
                      wrote on last edited by Crawl.W
                      #10

                      @sgaist I got it, I needn't have implement operator for ExpAnalysisResult, just delete it.

                      1 Reply Last reply
                      0

                      • Login

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