'Binary Operator overloading for a member enum class (with non static members)

The following code doesn't compile.

error: invalid use of non-static data member 'data'

#include <iostream>
#include <unordered_map>
#include <vector>
class Domain {
   public:
    enum class fieldname { x_, y_ };
    std::unordered_map<fieldname, std::vector<double>> data;
    // default constructor. Hard coding is only for this test!
    Domain() {
        data[Domain::fieldname::x_] = std::vector<double>{1, 23, 4};
        data[Domain::fieldname::y_] = std::vector<double>{1, 23, 4};
    }
    // operator overloading
    friend std::vector<double> operator+(fieldname one, fieldname two) {
        std::vector<double> result = data[one]; // so we get the right size
        for (int i = 0; i < result.size(); ++i) {
            result[i] = data[one][i] + data[two][i];
        }
        return result;
    }
};
int main() {
    Domain d;
    std::vector<double> temp = Domain::fieldname::x_ + Domain::fieldname::y_;
    for (auto item : temp) std::cout << item << std::endl;
    return 0;
}

I think it is evident from the code what I am trying to accomplish. Could someone suggest how the operator + can be overloaded so that the enum classes can be used as a proxy for vectors which are members of a class?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source