访问枚举 class 属性,这些属性在 C++ 函数内部的结构中定义

access enum class properties which defined in struct inside a function in c++

如何访问在枚举 class 中定义的结构属性。 错误是 "error: 'e' is not a class,namespace,or enumeration"

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;
}

您需要的是:

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;
}