Why compile error “Use of unassigned local variable”?

前端 未结 10 2208
说谎
说谎 2020-11-22 03:16

My code is the following

int tmpCnt;  
if (name == \"Dude\")  
   tmpCnt++;  

Why is there an error Use of unassigned local variabl

10条回答
  •  灰色年华
    2020-11-22 03:58

    The default value table only applies to initializing a variable.

    Per the linked page, the following two methods of initialization are equivalent...

    int x = 0;
    int x = new int();
    

    In your code, you merely defined the variable, but never initialized the object.

提交回复
热议问题