Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error C2196: case value '?' already used

Tags:

c++

c

Ok, curious error while building with Visual Studio Ultimate 2012 (probably issue with ANSI, unicode etc) in the code...

switch (input[index])
{
    case 'א': // Alef Hebrew character
        if (/*conditional*/) 
        {
            // Do stuff.
        }
    break;

    case 'ב': // Beth Hebrew character
        if (/*conditional*/)
        {
            //Do stuff
        }
    break;

    default:
    {
            //Do some other stuff.
    }
    break;

}

The second case parameter generates...

Error C2196: case value '?' already used

Simple fix if possible.

like image 480
loumbut5 Avatar asked Jan 05 '23 10:01

loumbut5


1 Answers

Assuming input is an array of wchar_ts, your problem is that you're comparing a wide character to a narrow character literal.

As PeterT said in the comments:

If you save the file as utf-8 encoded then א is 0xD790 and ב is 0xD791, so if input[index] is of type char both would try to match 0xD7.

That's why you're getting the error you mentioned. (char has enough space to store an ASCII value, and the rest is omitted)


You can fix this by prefixing your literals with a capital L (turning them in to wide characters).

case L'א': // Alef Hebrew character
    if (/*conditional*/) 
    {
        // Do stuff.
    }
break;

case L'ב': // Beth Hebrew character
    if (/*conditional*/)
    {
        //Do stuff
    }
break;

Also, you need to make sure your source file is saved with unicode encoding & your compiler knows how to work with that.

Alternatively, you can simply escape the unicode value like so:

case L'\u05D0': // Aleph Hebrew character
// ...
case L'\u05D1': // Beth Hebrew character
like image 126
Ivan Rubinson Avatar answered Jan 12 '23 10:01

Ivan Rubinson