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. How to define a QBitArray variable in a struct?
Forum Updated to NodeBB v4.3 + New Features

How to define a QBitArray variable in a struct?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.1k 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
    mohmo
    wrote on 8 Nov 2014, 15:08 last edited by
    #1

    I define a struc like below and then i make an array of this structure:

    struct mm{QBitArray bb; int val;};
    mm* vec=new mm [10];

    then i want to set a bit:
    u [7].bb.setBit(3,1);

    I encounter a crash message.
    whats wrong with this definition?

    1 Reply Last reply
    0
    • C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 8 Nov 2014, 17:31 last edited by
      #2

      Is u the same as vec ?
      Do you set the size of the bit array somewhere? If not then accessing bit 3 of array of size 0 will obviously be undefined behavior (crash in your case).

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohmo
        wrote on 8 Nov 2014, 18:42 last edited by
        #3

        yeah sorry:
        vec [7].bb.setBit(3,1);
        but i cant set the length in the struct!:
        struct mm{QBitArray bb(8); int val;};
        this causes error.
        How do you define a bitarray in a structure?

        1 Reply Last reply
        0
        • C Online
          C Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on 8 Nov 2014, 18:56 last edited by
          #4

          With that syntax you're trying to call a function(a constructor). You can't call a function at a declaration. It's a compile-time construct.

          You have several choices:
          @
          //if your compiler supports c++11 use uniform initialization:
          struct mm{
          QBitArray bb{8};
          int val;
          };

          //if it doesn't you can either define a constructor:
          struct mm{
          mm() : bb(8) {}
          QBitArray bb;
          int val;
          };

          //or just resize the thing later on:
          struct mm{
          QBitArray bb;
          int val;
          };
          //somewhere later:
          mm foo;
          foo.bb.resize(8);
          @

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mohmo
            wrote on 9 Nov 2014, 16:40 last edited by
            #5

            Thanks chris
            if we choose your third approach, HOW can i define the structure mm on HEAP and in the form of an ARRAY??
            like:
            mm* foo=new foo [10]; or something like that.

            1 Reply Last reply
            0
            • C Online
              C Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on 9 Nov 2014, 16:47 last edited by
              #6

              new mm, not new foo. Then you would resize each element in a for loop:
              @
              mm* foo = new mm[10];
              for(int i=0; i< 10; ++i)
              foo[i].bb.resize(8);
              @
              But I would favor the first option. the loop is then unnecessary.

              1 Reply Last reply
              0

              1/6

              8 Nov 2014, 15:08

              • Login

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