Aaron
Aaron

Reputation: 4480

Trouble using enum in a set c++

I put all my enums into a set. I am trying to iterate through the set and use a switch statement to give variables values(haven't done that yet). I am having trouble setting up an iterator to put in the switch statement that will tell me which enum I am using. Here is my code. The *it is giving me a null value.

    for(std::set<CreateAndUseIniFile::iniFileValues>::iterator it=m_modules.begin(); it!=m_modules.end(); ++it)
{
    switch(*it)
    {
    case CreateAndUseIniFile::VOLTAGE1_OFFSET:
        break;
    case CreateAndUseIniFile::VOLTAGE12_OFFSET:
        break;
    case CreateAndUseIniFile::VOLTAGE123_OFFSET:
        break;
     }
}

Here is the code where I added to the set

    m_modules.insert(CreateAndUseIniFile::VOLTAGE1_OFFSET);
m_modules.insert(CreateAndUseIniFile::VOLTAGE12_OFFSET);
m_modules.insert(CreateAndUseIniFile::VOLTAGE123_OFFSET);

Here is the enum

class CreateAndUseIniFile {
 public:

enum iniFileValues
{
    VOLTAGE1_OFFSET,
    VOLTAGE12_OFFSET,
    VOLTAGE123_OFFSET
    }
  std::set<CreateAndUseIniFile::iniFileValues> m_modules;
};

Upvotes: 0

Views: 94

Answers (1)

4pie0
4pie0

Reputation: 29724

Your container should be:

std::set< CreateAndUseIniFile::iniFileValues>

and iterator

std::set< CreateAndUseIniFile::iniFileValues>::iterator it;

And it will work as expected.

Note: switch may be optimazed, so you will not be able to step into until you add something more to switch cases, for instance:

for ( std::set< CreateAndUseIniFile::iniFileValues>::iterator it = s.begin(); 
                                               it != s.end(); ++it) {
    switch (*it) {
        case CreateAndUseIniFile::VOLTAGE1_OFFSET:
        {
            int iu = 100;
            break;
        }
        case CreateAndUseIniFile::VOLTAGE12_OFFSET:
        {
            int u = 1000;
            break;
        }
        //...
    }
}

Upvotes: 1

Related Questions