Having problems with accessing struct

微笑、不失礼 提交于 2019-12-20 07:32:55

问题


I think i may be losing it but can anyone double check my sanity? This is the only code i wrote in a new file to see my project file is not messed up.

Error: This declaration has no storage class or type specifier

Error: Expected a ";"


回答1:


On a global level you can only have declarations and definitions, not statements (like g.a = 1; is) or expressions.




回答2:


Also why not use by static initialization?

Game g = { 1 };



回答3:


Have some function to have the executable statement, for example,

Game Init() {
    Game result;
    result.a = 1; // g is global
    return result;
}

Game g = Init();

Even better to have a class called Game and have the constructor to do the initialization.

class Game {
  int a;
  public:  
  Game(int a_):a(a_){}
};

Game g(1);


来源:https://stackoverflow.com/questions/23054065/having-problems-with-accessing-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!