C++ '=' 标记前声明中的限定 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26454099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:36:15  来源:igfitidea点击:

Qualified-id in declaration before '=' token

c++opencvstaticdeclaration

提问by tetris11

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.

我有两个公开课;一个静态(DesktopOps),一个非静态(Args),我正在尝试初始化 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:

这是一个 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);
}

回答by jpo38

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

但是这个全局 Args 变量没有意义......

回答by Some programmer dude

In the structure you declarethe member variables, but when you definethem 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 argvariable in the mainfunction. This means you have to do the definition and the initialization in two steps:

不幸的是,这无法完成,因为初始化取决于函数中的局部arg变量main。这意味着您必须分两步进行定义和初始化:

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);
}