Important: Please read the Qt Code of Conduct -

why cant i access my struct?

  • 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?

  • @haxxe

    You are missing explicitly initialization for static member.

    like this: int another_variable::v1 = 0;

  • @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?

  • Lifetime Qt Champion

    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 :)

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

  • Lifetime Qt Champion

    @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.

  • 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.

  • Lifetime Qt Champion

    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.

  • 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.

  • Lifetime Qt Champion

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

Log in to reply