Advertisement

why cant i assign a value to a variable when its created

Started by June 22, 2002 01:48 AM
11 comments, last by Shenron 22 years, 4 months ago
im wondering why i get a compiler error when i do this saying see declaration of "variable name." as far as i know i thought this was completely legal. i get a error with msvc6 when i do this:

case WM_MOUSEMOUSE:

int x = (int)LOWORD(lParam);
int y = (int)HIWORD(lParam);
  
so instead i have to declare the variable first and assign a value after which is a waste of space like this:

int x, y;
x = (int)LOWORD(lParam);
y = (int)HIWORD(lParam);
  
what am i doing wrong? [edited by - Shenron on June 22, 2002 2:50:01 AM]
You should avoid declaring variables within a case: statement, unless you actually have a { block }.

And don't worry about 'space'.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 22, 2002 2:53:53 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
the { and } solved it thanks a bunch

Shenron
Something to consider - if your compiler is standards compatible - those declarations go to the bit bucket as soon as the case is down. The leave scope and should vanish...


God was my co-pilot but we crashed in the mountains and I had to eat him...
Landsknecht
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
Landsknecht, are you sure about that? I haven''t heard of such a thing before.
That is the purpose of brackets. They define a scope. Take this for example:
for(int i = 0; i < 100; i++){   int l = 5;   FooBar(i, l);}cout << i;  // If compiler is standards compliant - this will fail, cause i has left scope...cout << l;  // This should also fail...  


P.S. And BTW - MSVC++ 6.0 is **NOT** compliant 100%. In this example, i is still valid but l will fail...

God was my co-pilot but we crashed in the mountains and I had to eat him...
Landsknecht

[edited by - landsknecht on June 22, 2002 5:40:38 AM]
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
Advertisement
Your earlier post seemed to state that a case statement should limit scope, according to the standard. Now you are explaining that braces limit scope, which I already know (I''d be worried if I didn''t).

As far as I know, a case statement is a label, and a switch statement is like a bunch of conditional gotos - and code like this should work:

  switch(foo) {  case 0:  if(0) {  case 1:    MessageBox(0, "equal to 1", "", MB_OK);           }           break;  default: MessageBox(0, "neither 0 nor 1", "", MB_OK);}  
quote: Original post by Landsknecht
That is the purpose of brackets. They define a scope. Take this for example:

<example omitted>

That''s an MSVC bug with for, not scope in general.

quote:
Something to consider - if your compiler is standards compatible - those declarations go to the bit bucket as soon as the case is down. The leave scope and should vanish...

case doesn''t define scope.
switch( x ){case 0:case 1:  int i;} 

The compiler complains in the above that there is a potential for the declaration of i to be skipped and asks you to wrap it in scope delimiters - curly braces.
What the...? <- referring to Beer Hunter's code.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 22, 2002 2:54:55 PM]
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
I phrased it slightly wrong. Case may not define a scope, but a switch sure does. This does not work exactly because switch defines a scope...

int foo = 1;switch(foo){case 1:{   int bar = 5;   break;}}cout << bar << endl; 


This gives you this error:
test.cpp(17) : error C2065: ''bar'' : undeclared identifier

Tell me that is not a defined scope.


God was my co-pilot but we crashed in the mountains and I had to eat him...
Landsknecht
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.

This topic is closed to new replies.

Advertisement