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. confusing error
Forum Updated to NodeBB v4.3 + New Features

confusing error

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 414 Views 1 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.
  • M Offline
    M Offline
    MScottM
    wrote on 11 Mar 2023, 17:13 last edited by
    #1

    I'm trying to use a function that takes a QByteArray* and returns QInt32. The IDE has an error message: "'this' argument to member function 'byteArrayToUint32' has type 'const MainWindow', but function is not marked const".

    Marking the function (or anything else for that matter) as const doesn't change anything, so I'm wondering what the actual issue might be.

    Here are my declarations in MainWindow.h:

    QByteArray *ba;
    qint32 byteArrayToUint32(QByteArray &bytes);
    
    

    and where I call the function in MainWindow.cpp:

    QByteArray fbytes;
    ba->resize(query.size());
    fbytes = byteArrayToUint32(ba);  // <- error is marked here
    

    and the function in MainWindow.cpp:

    quint32 byteArrayToUint32(QByteArray &bytes)
    {
        auto count = bytes.size();
        if (count == 0 || count > 4) {
            return 0;
        }
        quint32 number = 0U;
        for (int i = 0; i < count; ++i) {
            auto b = static_cast<quint32>(bytes[count - 1 - i]);
            number += static_cast<quint32>(b << (8 * i));
        }
        return number;
    }
    
    C C S 3 Replies Last reply 11 Mar 2023, 17:44
    0
    • M MScottM
      11 Mar 2023, 17:13

      I'm trying to use a function that takes a QByteArray* and returns QInt32. The IDE has an error message: "'this' argument to member function 'byteArrayToUint32' has type 'const MainWindow', but function is not marked const".

      Marking the function (or anything else for that matter) as const doesn't change anything, so I'm wondering what the actual issue might be.

      Here are my declarations in MainWindow.h:

      QByteArray *ba;
      qint32 byteArrayToUint32(QByteArray &bytes);
      
      

      and where I call the function in MainWindow.cpp:

      QByteArray fbytes;
      ba->resize(query.size());
      fbytes = byteArrayToUint32(ba);  // <- error is marked here
      

      and the function in MainWindow.cpp:

      quint32 byteArrayToUint32(QByteArray &bytes)
      {
          auto count = bytes.size();
          if (count == 0 || count > 4) {
              return 0;
          }
          quint32 number = 0U;
          for (int i = 0; i < count; ++i) {
              auto b = static_cast<quint32>(bytes[count - 1 - i]);
              number += static_cast<quint32>(b << (8 * i));
          }
          return number;
      }
      
      C Offline
      C Offline
      Cobra91151
      wrote on 11 Mar 2023, 17:44 last edited by Cobra91151 3 Nov 2023, 18:35
      #2

      @MScottM

      Hello!

      I would suggest you use the QByteArray on the stack.

      Code:

      .h

      QByteArray ba;
      

      .cpp

      QByteArray fbytes;
      ba.resize(query.size());
      fbytes.append(byteArrayToUint32(ba));
      

      Test it and reply.

      1 Reply Last reply
      0
      • M MScottM
        11 Mar 2023, 17:13

        I'm trying to use a function that takes a QByteArray* and returns QInt32. The IDE has an error message: "'this' argument to member function 'byteArrayToUint32' has type 'const MainWindow', but function is not marked const".

        Marking the function (or anything else for that matter) as const doesn't change anything, so I'm wondering what the actual issue might be.

        Here are my declarations in MainWindow.h:

        QByteArray *ba;
        qint32 byteArrayToUint32(QByteArray &bytes);
        
        

        and where I call the function in MainWindow.cpp:

        QByteArray fbytes;
        ba->resize(query.size());
        fbytes = byteArrayToUint32(ba);  // <- error is marked here
        

        and the function in MainWindow.cpp:

        quint32 byteArrayToUint32(QByteArray &bytes)
        {
            auto count = bytes.size();
            if (count == 0 || count > 4) {
                return 0;
            }
            quint32 number = 0U;
            for (int i = 0; i < count; ++i) {
                auto b = static_cast<quint32>(bytes[count - 1 - i]);
                number += static_cast<quint32>(b << (8 * i));
            }
            return number;
        }
        
        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 11 Mar 2023, 18:04 last edited by
        #3

        @MScottM said in confusing error:

        QByteArray fbytes;
        ba->resize(query.size());
        fbytes = byteArrayToUint32(ba); // <- error is marked here

        fbytes is a QByteArray, byteArrayToUint32() returns an quint32
        ba is a pointer, byteArrayToUint32 takes a reference
        ba gets resized but never initialized, you access the data of this container (when it would compile) inside byteArrayToUint32() so you will get garbage.

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

        M 1 Reply Last reply 12 Mar 2023, 16:51
        2
        • C Christian Ehrlicher
          11 Mar 2023, 18:04

          @MScottM said in confusing error:

          QByteArray fbytes;
          ba->resize(query.size());
          fbytes = byteArrayToUint32(ba); // <- error is marked here

          fbytes is a QByteArray, byteArrayToUint32() returns an quint32
          ba is a pointer, byteArrayToUint32 takes a reference
          ba gets resized but never initialized, you access the data of this container (when it would compile) inside byteArrayToUint32() so you will get garbage.

          M Offline
          M Offline
          MScottM
          wrote on 12 Mar 2023, 16:51 last edited by
          #4

          @Christian-Ehrlicher this is where my lack of formal training is shining through... but, thanks to your reply, I have figured out I am going about what I am trying to do in the wrong way. I'm going to have to redo this bit of code somehow.

          Marking this solved - even though it's more like...a lesson learned (maybe).

          1 Reply Last reply
          0
          • M MScottM has marked this topic as solved on 12 Mar 2023, 16:52
          • M MScottM
            11 Mar 2023, 17:13

            I'm trying to use a function that takes a QByteArray* and returns QInt32. The IDE has an error message: "'this' argument to member function 'byteArrayToUint32' has type 'const MainWindow', but function is not marked const".

            Marking the function (or anything else for that matter) as const doesn't change anything, so I'm wondering what the actual issue might be.

            Here are my declarations in MainWindow.h:

            QByteArray *ba;
            qint32 byteArrayToUint32(QByteArray &bytes);
            
            

            and where I call the function in MainWindow.cpp:

            QByteArray fbytes;
            ba->resize(query.size());
            fbytes = byteArrayToUint32(ba);  // <- error is marked here
            

            and the function in MainWindow.cpp:

            quint32 byteArrayToUint32(QByteArray &bytes)
            {
                auto count = bytes.size();
                if (count == 0 || count > 4) {
                    return 0;
                }
                quint32 number = 0U;
                for (int i = 0; i < count; ++i) {
                    auto b = static_cast<quint32>(bytes[count - 1 - i]);
                    number += static_cast<quint32>(b << (8 * i));
                }
                return number;
            }
            
            S Offline
            S Offline
            SimonSchroeder
            wrote on 13 Mar 2023, 08:53 last edited by
            #5

            @MScottM said in confusing error:

            The IDE has an error message: "'this' argument to member function 'byteArrayToUint32' has type 'const MainWindow', but function is not marked const".

            Here is another lesson to learn: The error tells us that byteArrayToUint32 is a member function of MainWindow. And the error also tells us that you are calling this function from within another function that is marked as const. For member functions everything after the argument list applies to the implicit this pointer in member functions. And even though you are not writing it explicitly you are actually calling this->byteArrayToUint32(ba). If the this pointer is const it can only call member functions that are also marked as const. So, the easy solution is to declare your function const: quint32 byteArrayToUint32(QByteArray &bytes) const;

            However, your conversion function actually does not need the this pointer in any way. You can either have it as a free-standing function (declared outside of your class). This might pollute your global namespace unnecessarily. Another approach is to mark it static. The latter would be the general approach I'd prefer.

            1 Reply Last reply
            0

            5/5

            13 Mar 2023, 08:53

            • Login

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