访问在c ++中的函数内的struct中定义的枚举类属性

时间:2015-01-25 20:33:41

标签: c++ struct enums

如何访问枚举类中定义的struct属性。 错误是"错误:' e'不是类,命名空间或枚举"

enum class pay_type
{
    hourly, salary
};

struct employee
{
    string first_name;        
    string last_name;         
    pay_type pay_kind;        
    double pay_unit_amount;   
};

istream& operator >> (std::istream& is, employee& e)
{

    is >> e.first_name;
    is >> e.last_name;
    if (e.pay_kind == pay_type::hourly && e.pay_kind == pay_type::salary)
    {
        is >> e::pay_kind;
    }

    is >> e.pay_unit_amount;
    return is;
}

1 个答案:

答案 0 :(得分:1)

您需要的是:

std::istream& operator >> (std::istream& is, employee& e)
{

    is >> e.first_name;
    is >> e.last_name;

    // Read the pay kind as an integer.
    // Check to make sure that the value is acceptable.

    int payKind;
    is >> payKind;    
    if (payKind == pay_type::hourly || payKind == pay_type::salary)
    {
       e.pay_kind = static_cast<pay_type>(payKind);
    }
    else
    {
       // Deal with error condition
    }

    is >> e.pay_unit_amount;
    return is;
}