“Y does not name a type” error in C++

前端 未结 2 614
不知归路
不知归路 2020-12-06 05:48

I don\'t know what to search to find an explanation for this, so I am asking.
I have this code which reports error:

struct Settings{
    int width;
    i         


        
2条回答
  •  失恋的感觉
    2020-12-06 06:01

    In C++11 you can write

    struct Settings {
        int width;
        int height;
    } settings = { 800, 600 };
    

    in order to fix your bug. The error appears because you're trying to assign a value outside a function body. You can initialize but not assign global data outside of a function.

    EDIT:

    Concerning your edit, just write

    Settings settings = {{800, 600}, {10, 20, 3}};
    

    I'm not 100% sure, if this works though, because of the inheritance. I would recommend to avoid inheritance in this case and write the Dimensions as member data into your Build_menu structure. Inheritance will sooner or later give you all kinds of trouble, when used this way. Prefer composition to inheritance. When you do that, it's gonna look like

    Settings settings = {{800, 600}, {{10, 20}, 3}};
    

提交回复
热议问题