我有两个公开课;一个静态( DesktopOps ),一个非静态( Args ),我正在尝试初始化 main 中静态类的静态变量。

我不断收到的错误消息是:

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

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

最佳答案

我真的不明白你在做什么......但是静态变量必须在全局范围内创建,在主函数之外:

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

int main(){

}

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

关于c++ - '=' token 前声明中的合格 ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26454099/

10-13 07:04