'='标记之前的声明中的Qualified-id

时间:2014-10-19 18:48:53

标签: c++ opencv static declaration

我有两个公共课;一个静态(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);
}

2 个答案:

答案 0 :(得分:14)

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

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

答案 1 :(得分:7)

声明成员变量的结构中,但是当定义它们时,你不能在函数中执行它,它必须在全局范围内完成,像

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

不幸的是,由于初始化取决于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);
}