13

I have two public classes; one static (DesktopOps), one non-static (Args), and I'm trying to initialise the static variables of the static class in main.

The error message I keep getting is:

main.cpp:25: error: qualified-id in declaration before '=' token
     Point DesktopOps::window_coords = Point(arg.topleft_x, arg.topleft_y);
                                     ^
main.cpp:26: error: qualified-id in declaration before '=' token
     Point DesktopOps::window_dims = Point(arg.width, arg.height);
                                   ^

Here's a MWE:

#include <opencv2/opencv.hpp>

using namespace cv;

struct Args{
    int topleft_x, topleft_y, width, height;

    Args(){
        topleft_x = topleft_y = width = height = -1;
    }
};


struct DesktopOps {
    static Point window_coords;
    static Point window_dims;

};



int main(){
    Args arg();

    Point DesktopOps::window_coords = Point(arg.topleft_x, arg.topleft_y);
    Point DesktopOps::window_dims = Point(arg.width, arg.height);
}
tetris11
  • 817
  • 2
  • 11
  • 27

2 Answers2

16

I don't really understand what you are trying to do....but static variables must be created in global scope, outside the main function:

Args arg;
Point DesktopOps::window_coords = Point(arg.topleft_x, arg.topleft_y);
Point DesktopOps::window_dims = Point(arg.width, arg.height);

int main(){

}

But this global Args variable does not make sense....

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • I feel like an idiot for not knowing that static must be declared globally (how else will all members see it, doy), but what variable doesn't make sense? (I deleted a lot of intermediate code btw) – tetris11 Oct 19 '14 at 19:04
  • 1
    I mean that having to declare a global Args variable using default ctor just to use the members in static window_coords and window_dims initialization just fixes your compilation bug but is a very ugly way to do it....I'm prettry sure you'll arrange your code better. – jpo38 Oct 19 '14 at 19:13
  • well... here's the thing, Arg acually takes as `(int argc, char arv ***)` as arguments, so it does need to be within main.... but I'll figure something out – tetris11 Oct 19 '14 at 19:54
  • 1
    You may just create `DesktopOps::window_coords` and `DesktopOps::window_dims` as `Point(0,0);` and modify them in your first lines of main. `static` does not mean `const`.... – jpo38 Oct 19 '14 at 19:56
  • no wait... removing the `Point` type prefix the second time around solved the problem. I was redeclaring the same damn variables...once again I have failed at life itself – tetris11 Oct 19 '14 at 20:06
  • It should work. Within `main` function, `DesktopOps::window_coords.x = 3` for instance must modify the static variable. – jpo38 Oct 19 '14 at 20:07
  • OK, good to know your problem is fixed now. Thanks for the upvote. – jpo38 Oct 19 '14 at 20:08
10

In the structure you declare the member variables, but when you define them you can't do it in a function, it has to be done in the global scope, like

struct DesktopOps {
    static Point window_coords;
    static Point window_dims;
};

Point DesktopOps::window_coords = Point(someX, someY);
Point DesktopOps::window_dims = Point(someW, someH);

int main()
{
    // ...
}

Unfortunately this can't be done since the initialization depends on the local arg variable in the main function. This means you have to do the definition and the initialization in two steps:

struct DesktopOps {
    static Point window_coords;
    static Point window_dims;
};

Point DesktopOps::window_coords;
Point DesktopOps::window_dims;

int main()
{
    Args arg;

    DesktopOps::window_coords = Point(arg.topleft_x, arg.topleft_y);
    DesktopOps::window_dims = Point(arg.width, arg.height);
}

Since the C++17 standard, inline member variable definitions is now available:

struct DesktopOps {
    inline static Point window_coords = Point(someX, someY);
    inline static Point window_dims = Point(someW, someH);
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • wow, left to make a sandwich and came back with a concise and informative solution. I understand now, though I don't get why this is necessary; i.e. why doesn't the compiler just do this for me? – tetris11 Oct 19 '14 at 18:58
  • 1
    Isn't that `Arg arg()` a function declaration? – Mat Oct 19 '14 at 19:04
  • it's the constructor initialised to variable `arg`, no? – tetris11 Oct 19 '14 at 19:08
  • 1
    http://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work @tetris11 – Mat Oct 19 '14 at 19:10
  • Holy... I've never looked at it like that before, it really could be a function declaration! It doesn't flag errors though, but okay no more sloppy initializations – tetris11 Oct 19 '14 at 19:47
  • Thanks for the solution, worked for me – AZ123 Aug 01 '23 at 13:45