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 cant i access my struct?
Qt 6.11 is out! See what's new in the release blog

why cant i access my struct?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 3.2k 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.
  • H Offline
    H Offline
    haxxe
    wrote on last edited by
    #1

    Hi, i got a struct like this:

        typedef struct name{
            static int v1;
            int v2;	
            int v3;	
            int v4;	
        } another_variable;
    

    in the .cpp i want to access the static variable but the compiler says:
    "undefined reference to MainWindow::name::v1"
    i want to access it like this:

    name Name;
    Name.v1 = 0;
    

    it seems like that i can get to the "normal" ints, but not the static one, why?

    D 1 Reply Last reply
    0
    • H haxxe

      Hi, i got a struct like this:

          typedef struct name{
              static int v1;
              int v2;	
              int v3;	
              int v4;	
          } another_variable;
      

      in the .cpp i want to access the static variable but the compiler says:
      "undefined reference to MainWindow::name::v1"
      i want to access it like this:

      name Name;
      Name.v1 = 0;
      

      it seems like that i can get to the "normal" ints, but not the static one, why?

      D Offline
      D Offline
      Devopia53
      wrote on last edited by
      #2

      @haxxe

      You are missing explicitly initialization for static member.

      like this: int another_variable::v1 = 0;

      1 Reply Last reply
      2
      • H Offline
        H Offline
        haxxe
        wrote on last edited by haxxe
        #3

        @Devopia53
        in the .cpp or .h?
        If I do it right before my function it says something like:

        error: qualified-id in declaration before ‘=’ token
        

        and i is it right, that i use the another variable and not the name?

        EDIT: i guess i got it.
        Not in an function, just above everything i had to write:

        int MainWindow::another_variable::v1 = 0;
        
        

        now it compiles, but is this right?

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

          Hi
          Can i ask what you need that single static for ?

          int MainWindow::another_variable::v1 = 0;
          This is correct syntax for init a static class member. Yes
          in .cpp , global scope.

          now it compiles, but is this right?

          Yes its correct with syntax but statics are not often used so
          i do wonder what the goal of the design is :)

          1 Reply Last reply
          0
          • H Offline
            H Offline
            haxxe
            wrote on last edited by
            #5

            got it to work, just got rid of the static.
            actually, i only wanted to create this variable one time at all.

            mrjjM 1 Reply Last reply
            0
            • H haxxe

              got it to work, just got rid of the static.
              actually, i only wanted to create this variable one time at all.

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

              @haxxe said in why cant i access my struct?:

              Actually, i only wanted to create this variable one time at all.

              For a complex object, that is expensive to construct a static might be the solution but
              for an int, const might be better even it dont only create it only once but pr instance of the owning class.

              1 Reply Last reply
              1
              • H Offline
                H Offline
                haxxe
                wrote on last edited by
                #7

                yes, but right now its working and im happy with it :D thanks for all your help.
                Qt is awesome, as IDE, as Community, as everything.
                its kinda complex to me because i'm really fresh to programming, but i already love it.

                mrjjM 1 Reply Last reply
                1
                • H haxxe

                  yes, but right now its working and im happy with it :D thanks for all your help.
                  Qt is awesome, as IDE, as Community, as everything.
                  its kinda complex to me because i'm really fresh to programming, but i already love it.

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

                  @haxxe
                  Super
                  Yep I love Creator too. Very nice editor and Qt as GUI framework is the best i have tried.
                  Indeed, if you not used c++ super much, the full blown details when starting can be daunting but
                  you can always ask here. we also have a c++ section if not related to Qt as much.

                  1 Reply Last reply
                  2
                  • H Offline
                    H Offline
                    haxxe
                    wrote on last edited by
                    #9

                    l also get problems because i work together with a friend on my projects, and hes just a full c gui, not c++ and this causes problems sometimes. i tried several things, but stick with qt now. its just amazing that i can develop an desktop programm and with 2 clicks it runs already on my pi.

                    mrjjM 1 Reply Last reply
                    0
                    • H haxxe

                      l also get problems because i work together with a friend on my projects, and hes just a full c gui, not c++ and this causes problems sometimes. i tried several things, but stick with qt now. its just amazing that i can develop an desktop programm and with 2 clicks it runs already on my pi.

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

                      @haxxe
                      Yep, there is long way from c to c++ regarding program architecture.
                      But classes do offer a lot of benefits.

                      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