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

前端 未结 2 620
不知归路
不知归路 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:20

    You cannot put assignments outside the context of a function in C++. If you're puzzled by the fact that you sometimes saw the = symbol being used outside the context of a function, such as:

    int x = 42; // <== THIS IS NOT AN ASSIGNMENT!
    
    int main()
    {
        // ...
    }
    

    That's because the = symbol can be used for initialization as well. In your example, you are not initializing the data members width and height, you are assigning a value to them.

提交回复
热议问题