Why I can't declare a std::string type in a switch/case box?

This is a discussion on Why I can't declare a std::string type in a switch/case box? within the C++ Programming forums, part of the General Programming Boards category; Here is the code. Code: int main(int argc, char* argv[]){ int i=2; switch (i) { case 0: cout<<0<<endl; break; case ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why I can't declare a std::string type in a switch/case box?

    Here is the code.
    Code:
    int main(int argc, char* argv[]){                                                                                                                                            
        int i=2;                                                                                                                                                                 
        switch (i)                                                                                                                                                               
        {                                                                                                                                                                        
            case 0: 
                cout<<0<<endl;                                                                                                                                                   
                break;                                                                                                                                                           
            case 1:                                                                                                                                                              
                string test="";                                                                                                                                                  
                cout<<1<<endl;                                                                                                                                                   
                break;                                                                                                                                                           
            case 2:                                                                                                                                                              
                cout<<2<<endl;                                                                                                                                                   
                break;                                                                                                                                                           
            default:                                                                                                                                                             
                break;                                                                                                                                                           
        }                                                                                                                                                                        
                                                                                                                                                                                 
        return 0;                                                                                                                                                                
    }
    as you can see, when declare a string test it reports errors

    main.cc: In function `int main(int, char**)':
    main.cc:24: error: jump to case label
    main.cc:21: error: crosses initialization of `std::string test'
    main.cc:27: error: jump to case label
    main.cc:21: error: crosses initialization of `std::string test'
    main.cc:24: warning: destructor needed for `std::string test'
    main.cc:24: warning: where case label appears here
    main.cc:24: warning: (enclose actions of previous case statements requiring
    destructors in their own scope.)
    main.cc:27: warning: destructor needed for `std::string test'
    main.cc:27: warning: where case label appears here
    make: *** [main.o] Error 1
    I don't understand the errors, what is "jump to case label" ?
    why I "crosses initialization of `std::string test'" ?

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Interestingly, if I put the declaration in a box, it's OK

    Code:
    int main(int argc, char* argv[]){                                                                                                                                            
        int i=2;                                                                                                                                                                 
        switch (i)                                                                                                                                                               
        {                                                                                                                                                                        
            case 0:                                                                                                                                                              
                cout<<0<<endl;                                                                                                                                                   
                break;                                                                                                                                                           
            case 1:                                                                                                                                                              
            {                                                                                                                                                                    
                string test="";                                                                                                                                                  
            }                                                                                                                                                                    
                cout<<1<<endl;                                                                                                                                                   
                break;                                                                                                                                                           
            case 2:                                                                                                                                                              
                cout<<2<<endl;                                                                                                                                                   
                break;                                                                                                                                                           
            default:                                                                                                                                                             
                break;                                                                                                                                                           
        }                                                                                                                                                                        
                                                                                                                                                                                 
        return 0;                                                                                                                                                                
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's the way to do it (whenever you need to declare variables in a case). Otherwise test would be in scope in the following cases too, but you could jump over the declaration (and construction) of it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    Interesting little corner of the language.

    A switch case is the same as a labeled "goto" statement. You are not allowed to "goto" a location in a block following the construction point of a local variable, and you can't do it with a switch either.

    The problem is, the string is still in scope at case 2. Each labeled section of a switch statement is NOT its own scope. So going to case 2 would bring the variable into scope even though it has not been constructed.

    Placing it in a block creates a new scope, so that the variable's scope is limited to case 1.

    EDIT: Some code to help clarify:

    Code:
        goto foo;
        std::string x;
    foo:
        std::cout << x << std::endl;
    The declaration brings a variable 'x' into scope -- however, the line itself, which constructs the variable, is skipped by the goto statement. So x is used without being initialized. The same thing is happening here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21