jeudi 28 mai 2015

Using enumerated class values to call an array element in C++

My code:

Enumerations.h

#ifndef ENUMERATIONS_H
#define ENUMERATIONS_H

enum class Employees
{
    ERIC,
    RYAN,
    EMILY
};

#endif

Structs.h

struct Employee
{
    std::string name;
    double wage;
};

Review.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Enumerations.h"
#include "Structs.h"

Employee employeeArray[3];

    employeeArray[0] = { "Eric Sartor", 18.75 };
    employeeArray[1] = { "Ryan Ulch", 20.36 };
    employeeArray[2] = { "Emily Gover", 18.75 };

cout << employeeArray[Employees::RYAN].name;

So I'm trying to do something that I read in my C++ tutorial, where you call an array element (a struct) via an enumerated value. Earlier in the tutorial, it's recommended that if you compiler is C++11 compliant (which mine is) that is it better to use an enumerated class rather than a regular enumeration.

I'm noticing that when trying to call my element from my array via the Employees::RYAN enumerated value, it gives me an error that says "expression must have integral or unscoped enum type". If I remove the class keyword from my enumeration so it's just enum Employees and I change the array index to RYAN, it works fine. Am I missing something, or does this just not work with an enumerated class?

Hopefully I was clear enough. In the example on the tutorial, he actually DID NOT use an enumerated class, just a regular enumeration, even though he explicitly said earlier to always do that if you can...hopefully someone can clarify this for me!

Aucun commentaire:

Enregistrer un commentaire