Thread: ENUM's with Switch/Case

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    ENUM's with Switch/Case

    So, for my homework I need to create a linked list of 20 random "monsters" using a templated linked list. This will be created in the CTOR of the "Arena" class.

    Visual studio is giving me an error when trying to compile when entering the switch/case for which monster to create at random. It is happening for every case:

    error C2360: initialization of 'monsterTypeTroll' is skipped by 'case' label

    In the Arena header file, I have this ENUM:
    Code:
    enum MonsterType
    {
        troll = 1,
        ogre,
        spectre,
        dragon,
        giantScorpion,
        human,
        wyrm,//human/dragon
        spectralOgre,//ogre/specter
        giantSpider,
        wife//just for fun, wifey laughed :)
    };
    In the associated .cpp, in the CTOR, I have this:
    Code:
    //local variable declarations
        Random rdm;
        MonsterType type;
        //create 20 monster list here
        
        for( int i = 0; i < MONSTER_LIST_LENGTH; i++ )
        {
            type = static_cast<MonsterType>(rdm.GetRandNumb( 1, 10 ));
            
        
            //why is it skipping each case statement?
            switch( type )
            {
            case troll:
                Troll * monsterTypeTroll = new Troll;
                m_monsterList.PushBack( monsterTypeTroll ); 
                break;
            case ogre:
                Ogre * monsterTypeOgre = new Ogre;
                m_monsterList.PushBack( monsterTypeOgre ); 
                break;
            case spectre:
                Spectre * monsterTypeSpectre = new Spectre;
                m_monsterList.PushBack( monsterTypeSpectre ); 
                break;
            case dragon:
                Dragon * monsterTypeDragon = new Dragon;
                m_monsterList.PushBack( monsterTypeDragon ); 
                break;
            case giantScorpion:
                GiantScorpion * monsterTypeScorpion = new GiantScorpion;
                m_monsterList.PushBack( monsterTypeScorpion ); 
                break;
            case human:
                Human * monsterTypeHuman = new Human;
                m_monsterList.PushBack( monsterTypeHuman ); 
                break;
            case wyrm:
                Wyrm * monsterTypeWyrm = new Wyrm;
                m_monsterList.PushBack( monsterTypeWyrm ); 
                break;
            case spectralOgre:
                SpectralOgre * monsterTypeSpectralOgre = new SpectralOgre;
                m_monsterList.PushBack( monsterTypeSpectralOgre ); 
                break;
            case giantSpider:
                GiantSpider * monsterTypeSpider = new     GiantSpider;
                m_monsterList.PushBack( monsterTypeSpider ); 
                break;
            case wife:
                Wife * monsterTypeWife = new Wife;
                m_monsterList.PushBack( monsterTypeWife ); 
                break;
            }
            
        }

    I've tried using a simple int like "1" instead of "troll" or whatever. Not sure why this is happening. I thought I knew how this worked =/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This issue has to do with the scope of a switch. Let's remove the breaks and just consider:
    Code:
    switch( type )
    {
    case troll:
        #1
        Troll * monsterTypeTroll = new Troll;
        m_monsterList.PushBack( monsterTypeTroll );
    case ogre:
        #2
        Ogre * monsterTypeOgre = new Ogre;
        m_monsterList.PushBack( monsterTypeOgre );
    }
    Suppose that type == troll. Then at point #1, monsterTypeTroll is declared (and initialised), and it still exists at point #2. But if type == ogre, then point #1 is skipped, so monsterTypeTroll is not declared, but in theory you could have used monsterTypeTroll at point #2. That would be a problem since the variable's declaration and initialisation is skipped yet it is used.

    One solution for what you want to do is to use braces to introduce smaller blocks of scope:
    Code:
    switch( type )
    {
    case troll:
        {
            Troll * monsterTypeTroll = new Troll;
            m_monsterList.PushBack( monsterTypeTroll );
        }
        break;
    case ogre:
        {
            Ogre * monsterTypeOgre = new Ogre;
            m_monsterList.PushBack( monsterTypeOgre );
        }
        break;
    }
    But it would be simpler to just do without those variables:
    Code:
    switch( type )
    {
    case troll:
        m_monsterList.PushBack( new Troll );
        break;
    case ogre:
        m_monsterList.PushBack( new Ogre );
        break;
    }
    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

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Oh my gosh you're a genius. I love little tricks like that

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you using new in the first place?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Why are you using new in the first place?
    Proably to achieve polymorphism, and because of not knowing about (or wanting to use), a boost ptr_container.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Is there a way to achieve polymorphism without using "new" to dynamically allocate memory? Keep in mind I am a 3rd term student, and haven't been taught everything. I wouldn't mind a well thought out answer from you Elysia, instead of a short unhelpful quip.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Dynamic memory allocation isn't specifically required to achieve polymorphic behaviour, but the alternatives if you want to store pointers in a container are significantly more complicated (i.e. need to create an object and keep it alive for as long as the container holds its address) and therefore error prone. I therefore wouldn't recommend a beginner play with such techniques.

    Personally, I'd implement some form of the factory pattern in this case, rather than using a switch/case to decide what object to create. The factory approach doesn't offer much if you're only using a few (say, a handful) object types, but it is easier to maintain if the number of object types needed is larger.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Oh nice, thanks Grumpy. Did a google search on the factory pattern, looks like I have some reading to do

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Jeff Clark View Post
    ...I wouldn't mind a well thought out answer from you Elysia, instead of a short unhelpful quip.
    The first step towards helpful advice is to understand the situation and the requirements. Therefore, seeing as you really shouldn't use raw pointers, I enquired about why you did it so I could understand your situation better.
    To add to iMalc's answer, if you ever have need of pointers, consider using smart pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  2. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  3. Switch case help ?
    By Black&White in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2004, 12:04 PM
  4. switch using enum
    By eth0 in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2004, 03:51 PM
  5. switch case....
    By tetraflare in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2003, 03:52 PM

Tags for this Thread