mercredi 22 juin 2016

Operator overloading outside of a class in a namespace

I am trying to overload the comparison operator for a union class in a namespace to use it as a key in an unordered_map.

x.h:

#include <DirectXPackedVector.h>

namespace std
{
    template<> struct hash<DirectX::PackedVector::XMUBYTEN4>
    {
        std::size_t operator()(DirectX::PackedVector::XMUBYTEN4 const& s) const {
            std::size_t const h1(std::hash<uint8_t>()(s.x));
            std::size_t const h2(std::hash<uint8_t>()(s.y));
            std::size_t const h3(std::hash<uint8_t>()(s.z));
            std::size_t const h4(std::hash<uint8_t>()(s.w));
            return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
        }
    };
}

    std::unordered_map<DirectX::PackedVector::XMUBYTEN4, unsigned int> mc_province_index;

The hashing works fine but when i try to overload the comparison operator like so:

bool operator==(const DirectX::PackedVector::XMUBYTEN4 & lhs, const DirectX::PackedVector::XMUBYTEN4 & rhs) {
    if (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w)
        return true;
    return false;
}

I get a

C2678: binary '==' : no operator found which takes a left-hand operand of type 'const DirectX::PackedVector::XMUBYTEN4' (or there is no acceptable conversion)

I tried doing it inside the namespace

namespace DirectX{
    namespace PackedVector{
        bool operator==(const DirectX::PackedVector::XMUBYTEN4 & lhs, const DirectX::PackedVector::XMUBYTEN4 & rhs) {
            if (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w)
                return true;
            return false;
        }
    }
}

but this gives me a LNK2005 saying it is already defined in object file.

What am I doing wrong here and how can I overload this operator?

Aucun commentaire:

Enregistrer un commentaire