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);
}
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);
}
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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With