|
-
July 10th, 2008, 08:10 AM
#1
Assignment to global var. = Redefinition?
Hi
Why isn't assignment to global variables allowed?
Code:
int t;
t = 9; // error C2086: 'int t' : redefinition
int main()
{
return 0;
}
Thanks
-
July 10th, 2008, 08:15 AM
#2
Re: Assignment to global var. = Redefinition?
 Originally Posted by sawer
Hi
Why isn't assignment to global variables allowed?
Code:
int t;
t = 9; // error C2086: 'int t' : redefinition
int main()
{
return 0;
}
Thanks
Because executable statements have to be inside functions. Either:
Code:
int t = 9; // declaration and definition
int main()
{
}
or
Code:
int t; // declaration - initialised to 0 by default
int main()
{
t = 9; // assignment (executable statement, must be in a function)
}
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 10th, 2008, 08:29 AM
#3
Re: Assignment to global var. = Redefinition?
1, missing type specifier - int assumed. Note: C++ does not support default-int
2, 'int t' : redefinition
Becasue the "int assumed",so the compile think that is redefinition!
Cigagou,Cogitou!
-
July 10th, 2008, 09:53 AM
#4
Re: Assignment to global var. = Redefinition?
 Originally Posted by active2volcano
1, missing type specifier - int assumed. Note: C++ does not support default-int
Huh?
-
July 10th, 2008, 10:06 AM
#5
Re: Assignment to global var. = Redefinition?
You used to be able to leave out the type name in C and the compiler would assume you mean int:
Last edited by Zaccheus; July 11th, 2008 at 03:13 AM.
Reason: type name, not variable name !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|