Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qualified-id in declaration before '=' token

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);
}
like image 284
tetris11 Avatar asked Oct 19 '14 18:10

tetris11


2 Answers

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);
}
like image 93
Some programmer dude Avatar answered Nov 14 '22 07:11

Some programmer dude


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

like image 27
jpo38 Avatar answered Nov 14 '22 08:11

jpo38