opencv “=”标记之前的声明中的限定ID

nle07wnf  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(138)

我有两个公开课;一个static(DesktopOps),一个non-static(Args),我试图初始化main中static类的静态变量。
我一直收到的错误消息是:

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

toiithl6

toiithl61#

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

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变量没有意义。

unguejic

unguejic2#

在结构中,你声明了成员变量,但是当你定义它们的时候,你不能在函数中这样做,它必须在全局范围内完成,比如

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()
{
    // ...
}

字符串
不幸的是,这是不可能的,因为初始化依赖于main函数中的本地变量arg。这意味着你必须在两个步骤中完成定义和初始化:

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


自C++17标准以来,inline成员变量定义现在可用:

struct DesktopOps {
    inline static Point window_coords = Point(someX, someY);
    inline static Point window_dims = Point(someW, someH);
};

相关问题