Thread: Transfer of control bypasses initialization

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    30

    Transfer of control bypasses initialization

    Hi all,

    i have a switch command. and in case the user enters the command 's' (save), i create an output file, and save the data..
    but then a compile error was given as "transfer of control bypasses initialization of:"

    Code:
    
    
    Code:
    case 's':
                ofstream output_file;
                output_file.open(filename);
                            .... some code....
                output_file.close();
                break;
    case 'x':
                break;
            }


    what is the problem here, i need the output file only in this case, why is the compiler complaining and what is the solution ?

    thanks

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    740
    The output_file variable is still in scope at case 'x', but since the variable wouldn't be properly initialized if case 'x' is taken, the compiler complains about that. A simple fix is to put braces around the "body" of case 's', like this:

    Code:
    case 's':
    {
                ofstream output_file;
                output_file.open(filename);
                            .... some code....
                output_file.close();
                break;
    }
    case 'x':
                break;
            }


  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    740
    I would even rewrite it to something like this which also limits the scope of output_file:

    Code:
    case 's':
        if (ofstream output_file(filename)) {
            .... some code ....
        }
        break;
    case 'x':
        break;

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    Or stick the code in a function.

  5. #5
    Registered User
    Join Date
    Nov 2018
    Posts
    30
    Thank you guys, it works,
    although i thought, the compiler would see that i am not using output_file in case 'x' here und hence would not complain

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,998
    Quote Originally Posted by christop View Post
    I would even rewrite it to something like this which also limits the scope of output_file:

    Code:
    case 's':
        if (ofstream output_file(filename)) {
            .... some code ....
        }
        break;
    case 'x':
        break;
    You may need to use uniform initialisation syntax to get that to compile.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    740
    Quote Originally Posted by laserlight View Post
    You may need to use uniform initialisation syntax to get that to compile.
    Ah! So you do:

    Code:
    case 's':
        if (ofstream output_file{filename}) {
            .... some code ....
        }
        break;
    case 'x':
        break;
    (And I should've mentioned that this syntax requires C++11 or later, I believe, but that's already 9 years old at this point.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 07-03-2015, 12:28 PM
  2. Hosting a C# user control Hwnd within an ATL control
    By subdene in forum C++ Programming
    Replies: 0
    Last Post: 11-02-2013, 06:09 PM
  3. transfer
    By C-Compiler in forum Windows Programming
    Replies: 6
    Last Post: 11-23-2008, 08:07 PM
  4. Transfer control: GoTo?
    By wirefree101 in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2006, 05:55 PM
  5. file transfer
    By shamila in forum C++ Programming
    Replies: 5
    Last Post: 04-23-2002, 12:09 PM

Tags for this Thread