CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Assignment to global var. = Redefinition?

  1. #1
    Join Date
    Sep 2005
    Posts
    336

    Post 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

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Assignment to global var. = Redefinition?

    Quote 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


  3. #3
    Join Date
    Jul 2008
    Location
    dalian, China
    Posts
    36

    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!

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Question Re: Assignment to global var. = Redefinition?

    Quote Originally Posted by active2volcano
    1, missing type specifier - int assumed. Note: C++ does not support default-int
    Huh?

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Assignment to global var. = Redefinition?

    Huh?
    You used to be able to leave out the type name in C and the compiler would assume you mean int:

    Code:
    f(n)
    {
     return n;
    }
    Last edited by Zaccheus; July 11th, 2008 at 03:13 AM. Reason: type name, not variable name !
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)