C++ error “Case bypasses initialization of a local variable”

I came across this error when I tried to modify a complicated code this morning. I’d like to make sure it goes into eEvent4 condition, so I declared a string to record it in the log. And I got “Case bypasses initialization of a local variable”.

switch (eEvent)
{
case eEvent1:
case eEvent2:
case eEvent3:
case eEvent4:
string msg = "This is Event4";
Trace.TraceSimple(msg);
case eEvent5:
break;
}

After wonderful google, we need to add curly brackets {} to solve it.

switch (eEvent)
{
case eEvent1:
case eEvent2:
case eEvent3:
case eEvent4:
{
string msg = "This is Event4";
Trace.TraceSimple(msg);
}
case eEvent5:
break;
}

--

--

--

From Taiwan, a beautiful island. Learning English and sharing code experience.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Testing and Logging

7 things only minerstat can do

Install OPNSense and Linux on Cisco ASA

Automatic Deployment of Django App via Github actions

Level-up Data Minions!

THE FIRST YIELD FARM DeFi is growing fast and along with it, our team as well.

Build a REST API with Rust and MongoDB — Rocket Version

How to Create Github README as Resume?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jaime Lin

Jaime Lin

From Taiwan, a beautiful island. Learning English and sharing code experience.

More from Medium

Main features of abstract classes

REST vs WebSockets

BINARY HEAPS (with code in C++)

A Max-Heap example. Notice that it is a complete tree and parents’ keys are larger than children’s keys.

Temperature Control | Embedded Systems